Spring MVC框架select,option和options標籤的使用
阿新 • • 發佈:2019-02-14
程式碼:
selectForm.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>測試select標籤</title> </head> <body> <h3>form:select標籤直接新增form:option</h3> <form:form modelAttribute="user" method="post" action="selectForm"> <table> <tr> <td>部門:</td> <td> <form:select path="deptId"> <form:option value="1">財務部</form:option> <form:option value="2">開發部</form:option> <form:option value="3">銷售部</form:option> </form:select> </td> </tr> </table> </form:form> </body> </html>
selectForm2.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>測試select標籤</title> </head> <body> <h3>form:select標籤items屬性繫結Map</h3> <form:form modelAttribute="user" method="post" action="selectForm2"> <tr> <td>部門:</td> <td> <form:select path="deptId" items="${deptMap}"/> </td> </tr> </form:form> </body> </html>
selectForm3.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>測試options標籤</title> </head> <body> <h3>使用form:options標籤items屬性繫結Map</h3> <form:form modelAttribute="user" method="post" action="selectForm"> <table> <tr> <td>學歷:</td> <td> <form:select path="deptId"> <form:options items="${deptMap}"/> </form:select> </td> </tr> </table> </form:form> </body> </html>
selectForm4.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>測試select標籤</title>
</head>
<body>
<h3>使用form:options標籤繫結Object</h3>
<form:form modelAttribute="user" method="post" action="selectForm">
<table>
<tr>
<td>學歷:</td>
<td>
<form:select path="deptId">
<form:options items="${deptList}" itemLabel="name" itemValue="id"/>
</form:select>
</td>
</tr>
</table>
</form:form>
</body>
</html>
User.java
package com.bean;
import java.io.Serializable;
public class User implements Serializable {
// 部門編號
private Integer deptId;
public User() {
super();
// TODO Auto-generated constructor stub
}
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
}
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;
}
}
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.User;
@Controller
public class UserController {
@RequestMapping(value="/selectForm",method=RequestMethod.GET)
public String selectForm(Model model){
User user=new User();
// 設定deptId的值,頁面的select下拉框對應的option項會被選中
user.setDeptId(2);
model.addAttribute("user", user);
return "selectForm";
}
@RequestMapping(value="/selectForm2",method=RequestMethod.GET)
public String selectForm2(Model model){
User user=new User();
user.setDeptId(2);
// 頁面展現的可供選擇的select下拉框內容deptMap
Map<Integer, String> deptMap=new HashMap<Integer,String>();
deptMap.put(1, "財務部");
deptMap.put(2, "開發部");
deptMap.put(3, "銷售部");
model.addAttribute("user", user);
model.addAttribute("deptMap", deptMap);
return "selectForm2";
}
@RequestMapping(value="/selectForm3",method=RequestMethod.GET)
public String selectForm3(Model model) {
User user = new User();
user.setDeptId(2);
// 頁面展現的可供選擇的select下拉框內容deptMap
Map<Integer, String> deptMap = new HashMap<Integer, String>();
deptMap.put(1, "財務部");
deptMap.put(2, "開發部");
deptMap.put(3, "銷售部");
model.addAttribute("user", user);
model.addAttribute("deptMap", deptMap);
return "selectForm3";
}
@RequestMapping(value="/selectForm4",method=RequestMethod.GET)
public String selectForm4(Model model) {
User user=new User();
user.setDeptId(2);
// 頁面展現的可供選擇的select下拉框內容deptList,其中的元素的Dept物件
// 模擬從資料庫獲取到部門資訊封裝到物件當中
List<Dept> deptList=new ArrayList<Dept>();
deptList.add(new Dept(1, "財務部"));
deptList.add(new Dept(2, "開發部"));
deptList.add(new Dept(3, "銷售部"));
model.addAttribute("user", user);
model.addAttribute("deptList", deptList);
return "selectForm4";
}
}
截圖: