Spring MVC框架checkboxes标签的三种使用方式

来源:互联网 发布:手机airplay找不到mac 编辑:程序博客网 时间:2024/05/13 06:55

代码:

checkboxesForm.jsp

[html] view plain copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>测试checkboxes标签</title>  
  8. </head>  
  9. <body>  
  10. <h3>form:checkboxes测试</h3>  
  11. <form:form modelAttribute="user" method="post" action="checkboxesForm">  
  12.    <table>  
  13.       <tr>  
  14.          <td>选择课程:</td>  
  15.          <td>  
  16.             <form:checkboxes items="${courseList}" path="courses"/>  
  17.          </td>  
  18.       </tr>  
  19.    </table>  
  20. </form:form>  
  21. </body>  
  22. </html>  

checkboxesForm2.jsp

[html] view plain copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8. <title>测试checkboxes标签</title>  
  9. </head>  
  10. <body>  
  11. <h3>form:checkboxes测试</h3>  
  12. <form:form modelAttribute="user" method="post" action="checkboxesForm2" >  
  13.     <table>  
  14.         <tr>  
  15.             <td>选择课程:</td>  
  16.             <td>  
  17.                 <form:checkboxes items="${courseMap}" path="courses"/>  
  18.             </td>  
  19.         </tr>  
  20.     </table>  
  21. </form:form>  
  22. </body>  
  23. </html>  

checkboxesForm3.jsp

[html] view plain copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8. <title>测试checkboxes标签</title>  
  9. </head>  
  10. <body>  
  11. <h3>form:checkboxes测试</h3>  
  12. <form:form modelAttribute="employee" method="post" action="checkboxesForm3" >  
  13.     <table>  
  14.         <tr>  
  15.             <td>选择部门:</td>  
  16.             <td>  
  17.                 <form:checkboxes items="${deptList}" path="depts"   
  18.                     itemLabel="name" itemValue="id"/>  
  19.             </td>  
  20.         </tr>  
  21.     </table>  
  22. </form:form>  
  23. </body>  
  24. </html>  

User.java

[java] view plain copy
  1. package com.bean;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.List;  
  5.   
  6. public class User implements Serializable {  
  7.     private List<String> courses;  
  8.   
  9.     public User() {  
  10.         super();  
  11.         // TODO Auto-generated constructor stub  
  12.     }  
  13.   
  14.     public List<String> getCourses() {  
  15.         return courses;  
  16.     }  
  17.   
  18.     public void setCourses(List<String> courses) {  
  19.         this.courses = courses;  
  20.     }  
  21.       
  22. }  

Dept.java

[java] view plain copy
  1. package com.bean;  
  2.   
  3. public class Dept {  
  4.     private Integer id;  
  5.     private String name;  
  6.     public Dept() {  
  7.         super();  
  8.         // TODO Auto-generated constructor stub  
  9.     }  
  10.     public Dept(Integer id, String name) {  
  11.         super();  
  12.         this.id = id;  
  13.         this.name = name;  
  14.     }  
  15.     public Integer getId() {  
  16.         return id;  
  17.     }  
  18.     public void setId(Integer id) {  
  19.         this.id = id;  
  20.     }  
  21.     public String getName() {  
  22.         return name;  
  23.     }  
  24.     public void setName(String name) {  
  25.         this.name = name;  
  26.     }  
  27.       
  28. }  

Employee.java

[java] view plain copy
  1. package com.bean;  
  2.   
  3. import java.util.List;  
  4.   
  5. public class Employee {  
  6.     private List<Dept> depts;  
  7.   
  8.     public List<Dept> getDepts() {  
  9.         return depts;  
  10.     }  
  11.   
  12.     public void setDepts(List<Dept> depts) {  
  13.         this.depts = depts;  
  14.     }  
  15.       
  16. }  

UserController.java

[java] view plain copy
  1. package com.control;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import org.springframework.stereotype.Controller;  
  9. import org.springframework.ui.Model;  
  10. import org.springframework.web.bind.annotation.RequestMapping;  
  11. import org.springframework.web.bind.annotation.RequestMethod;  
  12.   
  13. import com.bean.Dept;  
  14. import com.bean.Employee;  
  15. import com.bean.User;  
  16.   
  17. @Controller  
  18. public class UserController {  
  19.     /* 
  20.      * 第一种方式 
  21.      */  
  22.     @RequestMapping(value="/checkboxesForm",method=RequestMethod.GET)  
  23.     public String registerForm(Model model){  
  24.         User user=new User();  
  25.         // 为集合变量courses添加“JAVAEE”和“Spring”,页面的checkbox复选框这两项会被选中  
  26.         List<String> list = new ArrayList<String>();  
  27.         list.add("JAVAEE");  
  28.         list.add("Spring");  
  29.         user.setCourses(list);  
  30.         // 页面展现的可供选择的复选框内容courseList  
  31.         List<String> courseList = new ArrayList<String>();  
  32.         courseList.add("JAVAEE");  
  33.         courseList.add("Mybatis");  
  34.         courseList.add("Spring");  
  35.         // model中添加属性user和courseList  
  36.          model.addAttribute("user",user);  
  37.          model.addAttribute("courseList",courseList);  
  38.          return "checkboxesForm";  
  39.     }  
  40.       
  41.     /* 
  42.      * 第二种方式 
  43.      */  
  44.     @RequestMapping(value="/checkboxesForm2",method=RequestMethod.GET)  
  45.     public String registerForm2(Model model){  
  46.         User user=new User();  
  47.         // 为集合变量courses添加“JAVAEE”和“Spring”,页面的checkbox复选框这两项会被选中  
  48.         List<String> list = new ArrayList<String>();  
  49.         list.add("1");  
  50.         list.add("3");  
  51.         user.setCourses(list);  
  52.         // 页面展现的可供选择的复选框内容courseList  
  53.         Map<String, String> courseMap = new HashMap<String, String>();  
  54.          courseMap.put("1","JAVAEE");  
  55.          courseMap.put("2","Mybatis");  
  56.          courseMap.put("3","Spring");  
  57.          // model中添加属性user和courseList  
  58.          model.addAttribute("user",user);  
  59.          model.addAttribute("courseMap",courseMap);  
  60.          return "checkboxesForm2";  
  61.     }  
  62.       
  63.     /* 
  64.      * 第三种方式 
  65.      */  
  66.      @RequestMapping(value="/checkboxesForm3",method=RequestMethod.GET)  
  67.      public String registerForm3(Model model) {  
  68.          Employee employee = new Employee();  
  69.          Dept dept = new Dept(1,"开发部");  
  70.          // 为集合变量depts添加Dept对象,该对象id=1,name=开发吧,页面的checkbox复选框这一项会被选中  
  71.          List<Dept> list = new ArrayList<Dept>();  
  72.          list.add(dept);  
  73.          employee.setDepts(list);  
  74.          // 页面展现的可供选择的复选框内容deptList  
  75.          List<Dept> deptList = new ArrayList<Dept>();  
  76.          deptList.add(dept);  
  77.          deptList.add(new Dept(2,"销售部"));  
  78.          deptList.add(new Dept(3,"财务部"));  
  79.          // model中添加属性employee和deptList  
  80.          model.addAttribute("employee",employee);  
  81.          model.addAttribute("deptList",deptList);  
  82.          return "checkboxesForm3";  
  83.      }  
  84. }  

截图:





0 0