Spring MVC框架checkboxes標籤的三種使用方式

 代碼:

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";
	 }
}

截圖:






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章