SpringMVC——checkboxes标签——集合元素为bean对象
SpringMVC配置文件参考:SpringMVC——form和input标签的使用:https://www.cnblogs.com/it-mh/articles/10570340.html

/FormTest/src/org/fkit/domain/Dept.java
package org.fkit.domain; public class Dept { private Integer id; private String name; public Dept() { super(); } public Dept(Integer id, String name) { super(); this.setId(id); this.setName(name); } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
/FormTest/src/org/fkit/domain/Employee.java
package org.fkit.domain; import java.util.List; public class Employee { private List<Dept> depts; public List<Dept> getDepts() { return depts; } public void setDepts(List<Dept> depts) { this.depts = depts; } }
/FormTest/src/org/fkit/controller/UserController.java
package org.fkit.controller; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.fkit.domain.Dept; import org.fkit.domain.Employee; import org.fkit.domain.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class UserController { @RequestMapping(value="/checkboxesform2",method=RequestMethod.GET) public String registerForm5(Model model) { Employee employee = new Employee(); Dept dept = new Dept(1,"开发部"); //为depts添加dept对象,页面checkbox复选框该项会被选中 List<Dept> list = new ArrayList<Dept>(); list.add(dept); employee.setDepts(list); //页面展示选项 List<Dept> deptList = new ArrayList<Dept>(); deptList.add(dept); deptList.add(new Dept(2,"销售部")); deptList.add(new Dept(3,"财务部")); //为model添加属性 model.addAttribute("employee", employee); model.addAttribute("deptList", deptList); return "content/checkboxesForm2.jsp"; } }
/FormTest/WebContent/content/checkboxesForm2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form:form modelAttribute="employee" method="post" action="checkboxes"> <table> <tr> <td>选择部门:</td> <td> <form:checkboxes items="${deptList}" path="depts" itemLabel="name" itemValue="id"/> </td> </tr> </table> </form:form> </body> </html>
测试结果:


浙公网安备 33010602011771号