Spring MVC框架checkboxes标签的三种使用方式
代码:
checkboxesForm.jsp
[html] view plain copy
- <%@ 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**>
- <**metahttp-equiv=”Content-Type”content=”text/html; charset=UTF-8”>**
- <**title**>测试checkboxes标签</**title**>
- </**head**>
- <**body**>
- <**h3**>form:checkboxes测试</**h3**>
- <**form:formmodelAttribute=”user”method=”post”action=”checkboxesForm”>**
- <**table**>
- <**tr**>
- <**td**>选择课程:</**td**>
- <**td**>
- <**form:checkboxesitems=”${courseList}“path=”courses”/>**
- </**td**>
- </**tr**>
- </**table**>
- </**form:form**>
- </**body**>
- </**html**>
checkboxesForm2.jsp
[html] view plain copy
- <%@ 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**>
- <**metahttp-equiv=”Content-Type”content=”text/html; charset=UTF-8”>**
- <**title**>测试checkboxes标签</**title**>
- </**head**>
- <**body**>
- <**h3**>form:checkboxes测试</**h3**>
- <**form:formmodelAttribute=”user”method=”post”action=”checkboxesForm2”>**
- <**table**>
- <**tr**>
- <**td**>选择课程:</**td**>
- <**td**>
- <**form:checkboxesitems=”${courseMap}“path=”courses”/>**
- </**td**>
- </**tr**>
- </**table**>
- </**form:form**>
- </**body**>
- </**html**>
checkboxesForm3.jsp
[html] view plain copy
- <%@ 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**>
- <**metahttp-equiv=”Content-Type”content=”text/html; charset=UTF-8”>**
- <**title**>测试checkboxes标签</**title**>
- </**head**>
- <**body**>
- <**h3**>form:checkboxes测试</**h3**>
- <**form:formmodelAttribute=”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
[java] view plain copy
- package com.bean;
- import java.io.Serializable;
- import java.util.List;
- public**class User implements** Serializable {
- private List
courses; - public User() {
- super();
- // TODO Auto-generated constructor stub
- }
- public List
getCourses() { - return courses;
- }
- public**void** setCourses(List
courses) { - this.courses = courses;
- }
- }
Dept.java
[java] view plain copy
- 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
[java] view plain copy
- package com.bean;
- import java.util.List;
- public**class** Employee {
- private List
depts; - public List
getDepts() { - return depts;
- }
- public**void** setDepts(List
depts) { - this.depts = depts;
- }
- }
UserController.java
[java] view plain copy
- 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
list = new ArrayList (); - list.add(“JAVAEE”);
- list.add(“Spring”);
- user.setCourses(list);
- // 页面展现的可供选择的复选框内容courseList
- List
courseList = new ArrayList (); - 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
list = new ArrayList (); - list.add(“1”);
- list.add(“3”);
- user.setCourses(list);
- // 页面展现的可供选择的复选框内容courseList
- Map
courseMap = new HashMap (); - 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
list = new ArrayList (); - list.add(dept);
- employee.setDepts(list);
- // 页面展现的可供选择的复选框内容deptList
- List
deptList = new ArrayList (); - 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”;
- }
- }
截图:
还没有评论,来说两句吧...