以user为例,user下有 username用户,password密码, address地址, receivePaper是否订阅, favotireFramework兴趣爱好,
user.java
public class User { private String username; private String password; private String address; private boolean receivePaper; private String [] favoriteFrameworks; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public boolean isReceivePaper() { return receivePaper; } public void setReceivePaper(boolean receivePaper) { this.receivePaper = receivePaper; } public String[] getFavoriteFrameworks() { return favoriteFrameworks; } public void setFavoriteFrameworks(String[] favoriteFrameworks) { this.favoriteFrameworks = favoriteFrameworks; } }
userController.java
import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import org.springframework.ui.ModelMap; @Controller public class UserController { @RequestMapping(value = "/user", method = RequestMethod.GET) public ModelAndView user() { User user = new User(); user.setFavoriteFrameworks((new String []{"Spring MVC","Struts 2"})); user.setGender("M"); ModelAndView modelAndView = new ModelAndView("user", "command", user); return modelAndView; } @RequestMapping(value = "/addUser", method = RequestMethod.POST) public String addUser(@ModelAttribute("SpringWeb")User user, ModelMap model) { model.addAttribute("username", user.getUsername()); model.addAttribute("password", user.getPassword()); model.addAttribute("address", user.getAddress()); model.addAttribute("receivePaper", user.isReceivePaper()); model.addAttribute("favoriteFrameworks", user.getFavoriteFrameworks()); model.addAttribute("gender", user.getGender()); return "userlist"; } @ModelAttribute("webFrameworkList") public List<String> getWebFrameworkList() { List<String> webFrameworkList = new ArrayList<String>(); webFrameworkList.add("Spring MVC"); webFrameworkList.add("Spring Boot"); webFrameworkList.add("Struts 2"); webFrameworkList.add("Apache Hadoop"); return webFrameworkList; } }
现在对userController.java进行说明:
public List<String> getWebFrameworkList()这一项是多选复选框的所有选项 ,@ModelAttribute("webFrameworkList")中的webFrameworkList是jsp页面中可以自己调用的属性变量.
而public ModelAndView user()代码块中的:
User user = new User(); user.setFavoriteFrameworks((new String []{"Spring MVC","Struts 2"}));
是默认勾选选。即一打开页面,Spring MVC合影Struts 2就好被勾选.
user.jsp
<%@ page contentType="text/html; charset=UTF-8"%> <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head> <title>Spring MVC表单处理(单选框)</title> </head> <body> <h2>用户信息 - </h2> <form:form method="POST" action="/RadioButton/addUser"> <table> <tr> <td><form:label path="username">用户名:</form:label></td> <td><form:input path="username" /></td> </tr> <tr> <td><form:label path="password">密码:</form:label></td> <td><form:password path="password" /></td> </tr> <tr> <td><form:label path="address">地址:</form:label></td> <td><form:textarea path="address" rows="5" cols="30" /></td> </tr> <tr> <td><form:label path="receivePaper">订阅新闻?</form:label></td> <td><form:checkbox path="receivePaper" /></td> </tr> <tr> <td><form:label path="favoriteFrameworks">喜欢的技术</form:label></td> <td><form:checkboxes items="${webFrameworkList}" path="favoriteFrameworks" /></td> </tr> <tr> <td colspan="2"> <input type="submit" value="提交"/> </td> </tr> </table> </form:form> </body> </html>
userlist.jsp代码
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page isELIgnored="false" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>用户信息显示</title> </head> <body> <table> <tr> <td>用户名</td> <td>${username}</td> </tr> <tr> <td>密码</td> <td>${password}</td> </tr> <tr> <td>地址</td> <td>${address}</td> </tr> <tr> <td>是否订阅</td> <td>${receivePaper}</td> </tr> <tr> <td>喜欢的技术</td> <td> <% String[] favoriteFrameworks = (String[])request.getAttribute("favoriteFrameworks"); for(String framework: favoriteFrameworks) { out.println(framework); } %> </td> </tr> </table> </body> </html>
注:解析变量一定要有一下标识:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page isELIgnored="false" %>