代码:
checkboxesForm.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 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>测试checkboxes标签</title>
- </head>
- <body>
- <h3>form:checkboxes测试</h3>
- <form:form modelAttribute="user" method="post" action="checkboxesForm">
- <table>
- <tr>
- <td>选择课程:</td>
- <td>
- <form:checkboxes items="${courseList}" path="courses"/>
- </td>
- </tr>
- </table>
- </form:form>
- </body>
- </html>
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 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>测试checkboxes标签</title>
- </head>
- <body>
- <h3>form:checkboxes测试</h3>
- <form:form modelAttribute="user" method="post" action="checkboxesForm2" >
- <table>
- <tr>
- <td>选择课程:</td>
- <td>
- <form:checkboxes items="${courseMap}" path="courses"/>
- </td>
- </tr>
- </table>
- </form:form>
- </body>
- </html>
checkboxesForm3.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 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>测试checkboxes标签</title>
- </head>
- <body>
- <h3>form:checkboxes测试</h3>
- <form:form modelAttribute="employee" method="post" action="checkboxesForm3" >
- <table>
- <tr>
- <td>选择部门:</td>
- <td>
- <form:checkboxes items="${deptList}" path="depts"
- itemLabel="name" itemValue="id"/>
- </td>
- </tr>
- </table>
- </form:form>
- </body>
- </html>
User.java
- package com.bean;
- import java.io.Serializable;
- import java.util.List;
- public class User implements Serializable {
- private List<String> courses;
- public User() {
- super();
- // TODO Auto-generated constructor stub
- }
- public List<String> getCourses() {
- return courses;
- }
- public void setCourses(List<String> courses) {
- this.courses = courses;
- }
- }
Dept.java
- package com.bean;
- public class Dept {
- private Integer id;
- private String name;
- public Dept() {
- super();
- // TODO Auto-generated constructor stub
- }
- public Dept(Integer id, String name) {
- super();
- this.id = id;
- this.name = 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;
- }
- }
Employee.java
- package com.bean;
- 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;
- }
- }
UserController.java
- package com.control;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import com.bean.Dept;
- import com.bean.Employee;
- import com.bean.User;
- @Controller
- public class UserController {
- /*
- * 第一种方式
- */
- @RequestMapping(value="/checkboxesForm",method=RequestMethod.GET)
- public String registerForm(Model model){
- User user=new User();
- // 为集合变量courses添加“JAVAEE”和“Spring”,页面的checkbox复选框这两项会被选中
- List<String> list = new ArrayList<String>();
- list.add("JAVAEE");
- list.add("Spring");
- user.setCourses(list);
- // 页面展现的可供选择的复选框内容courseList
- List<String> courseList = new ArrayList<String>();
- courseList.add("JAVAEE");
- courseList.add("Mybatis");
- courseList.add("Spring");
- // model中添加属性user和courseList
- model.addAttribute("user",user);
- model.addAttribute("courseList",courseList);
- return "checkboxesForm";
- }
- /*
- * 第二种方式
- */
- @RequestMapping(value="/checkboxesForm2",method=RequestMethod.GET)
- public String registerForm2(Model model){
- User user=new User();
- // 为集合变量courses添加“JAVAEE”和“Spring”,页面的checkbox复选框这两项会被选中
- List<String> list = new ArrayList<String>();
- list.add("1");
- list.add("3");
- user.setCourses(list);
- // 页面展现的可供选择的复选框内容courseList
- Map<String, String> courseMap = new HashMap<String, String>();
- courseMap.put("1","JAVAEE");
- courseMap.put("2","Mybatis");
- courseMap.put("3","Spring");
- // model中添加属性user和courseList
- model.addAttribute("user",user);
- model.addAttribute("courseMap",courseMap);
- return "checkboxesForm2";
- }
- /*
- * 第三种方式
- */
- @RequestMapping(value="/checkboxesForm3",method=RequestMethod.GET)
- public String registerForm3(Model model) {
- Employee employee = new Employee();
- Dept dept = new Dept(1,"开发部");
- // 为集合变量depts添加Dept对象,该对象id=1,name=开发吧,页面的checkbox复选框这一项会被选中
- List<Dept> list = new ArrayList<Dept>();
- list.add(dept);
- employee.setDepts(list);
- // 页面展现的可供选择的复选框内容deptList
- List<Dept> deptList = new ArrayList<Dept>();
- deptList.add(dept);
- deptList.add(new Dept(2,"销售部"));
- deptList.add(new Dept(3,"财务部"));
- // model中添加属性employee和deptList
- model.addAttribute("employee",employee);
- model.addAttribute("deptList",deptList);
- return "checkboxesForm3";
- }
- }
截图: