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

我就是我 2022-10-01 00:39 274阅读 0赞

代码:

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. <**metahttp-equiv=”Content-Type”content=”text/html; charset=UTF-8”>**
  7. <**title**>测试checkboxes标签</**title**>
  8. </**head**>
  9. <**body**>
  10. <**h3**>form:checkboxes测试</**h3**>
  11. <**form:formmodelAttribute=”user”method=”post”action=”checkboxesForm”>**
  12. <**table**>
  13. <**tr**>
  14. <**td**>选择课程:</**td**>
  15. <**td**>
  16. <**form:checkboxesitems=”${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. <**metahttp-equiv=”Content-Type”content=”text/html; charset=UTF-8”>**
  8. <**title**>测试checkboxes标签</**title**>
  9. </**head**>
  10. <**body**>
  11. <**h3**>form:checkboxes测试</**h3**>
  12. <**form:formmodelAttribute=”user”method=”post”action=”checkboxesForm2”>**
  13. <**table**>
  14. <**tr**>
  15. <**td**>选择课程:</**td**>
  16. <**td**>
  17. <**form:checkboxesitems=”${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. <**metahttp-equiv=”Content-Type”content=”text/html; charset=UTF-8”>**
  8. <**title**>测试checkboxes标签</**title**>
  9. </**head**>
  10. <**body**>
  11. <**h3**>form:checkboxes测试</**h3**>
  12. <**form:formmodelAttribute=”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. import java.io.Serializable;
  3. import java.util.List;
  4. public**class User implements** Serializable {
  5. private List courses;
  6. public User() {
  7. super();
  8. // TODO Auto-generated constructor stub
  9. }
  10. public List getCourses() {
  11. return courses;
  12. }
  13. public**void** setCourses(List courses) {
  14. this.courses = courses;
  15. }
  16. }

Dept.java

[java] view plain copy

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

Employee.java

[java] view plain copy

  1. package com.bean;
  2. import java.util.List;
  3. public**class** Employee {
  4. private List depts;
  5. public List getDepts() {
  6. return depts;
  7. }
  8. public**void** setDepts(List depts) {
  9. this.depts = depts;
  10. }
  11. }

UserController.java

[java] view plain copy

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

截图:

Center

Center 1

Center 2

发表评论

表情:
评论列表 (有 0 条评论,274人围观)

还没有评论,来说两句吧...

相关阅读