Spring MVC框架select,option和options标签的使用
2017-03-23 21:09
471 查看
代码:
selectForm.jsp
selectForm2.jsp
selectForm3.jsp
selectForm4.jsp
User.java
Dept.java
UserController.java
截图:
![](http://img.blog.csdn.net/20170323173240538?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSGFpZ2VfbGlu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20170323173250037?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSGFpZ2VfbGlu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20170323173259929?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSGFpZ2VfbGlu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
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"; } }
截图:
相关文章推荐
- Spring MVC框架select,option和options标签的使用
- Spring MVC 标签之select,option和options标签
- <html:select>,<html:option>,<html:options>,<html:optionsCollection>标签使用
- Spring MVC 标签之select,option和options标签
- <html:select>,<html:option>,<html:options>,<html:optionsCollection>标签使用
- 使用bBank.js解决IE下select标签innerHTML插入option的BUG
- 使用原生JavaScript实现对select增加option标签并附加value属性
- 解决如何通过循环来使用数据库的值设置jsp的select标签的option值
- Spring MVC框架checkbox标签的使用
- html:select,html:option,html:options标签
- <html:select>,<html:option>,<html:optionsCollection>标签使用
- Spring MVC框架form标签和input标签的使用
- Struts html 标签:checkbox multibox ridio select option options optionsCollection
- AngularJS v1.6.3,在select标签中使用ng-options无法触发ng-change事件
- Spring MVC框架errors标签的使用
- Spring MVC框架checkbox标签的使用
- struts中htmlselect标签和htmloptions 的使用
- <html:select>,<html:option>,<html:options>,<html:optionsCollection>标签使用
- struts中html:select标签和html:options 的使用(转)
- Spring MVC框架checkboxes标签的三种使用方式