會生成一個Html select標籤,綁定的items數據能夠是數組、集合或Map會根據items的內容生成select裏面的option選項,當path的值和items中的某條數據值相同的時候對應的option爲選定狀態,反之爲不選定狀態,用法與radiobuttons很類似。css
3、實例演示html
假設已經作好了SpringMVC的相關配置java
新建一個POJO類web
package com.demo.web.models; import java.util.List; import java.util.Map; public class TagsModel { private String username; private String password; private boolean testBoolean; private String[] selectArray; private String[] testArray; private Integer radiobuttonId; private Integer selectId; private List<Integer> selectIds; private Map<Integer, String> testMap; private String remark; public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } public void setTestBoolean(boolean testBoolean) { this.testBoolean = testBoolean; } public void setSelectArray(String[] selectArray) { this.selectArray = selectArray; } public void setTestArray(String[] testArray) { this.testArray = testArray; } public void setRadiobuttonId(Integer radiobuttonId) { this.radiobuttonId = radiobuttonId; } public void setSelectId(Integer selectId) { this.selectId = selectId; } public void setSelectIds(List<Integer> selectIds) { this.selectIds = selectIds; } public void setTestMap(Map<Integer, String> testMap) { this.testMap = testMap; } public void setRemark(String remark) { this.remark = remark; } public String getUsername() { return this.username; } public String getPassword() { return this.password; } public boolean getTestBoolean() { return this.testBoolean; } public String[] getSelectArray() { return this.selectArray; } public String[] getTestArray() { return this.testArray; } public Integer getRadiobuttonId() { return this.radiobuttonId; } public Integer getSelectId() { return this.selectId; } public List<Integer> getSelectIds() { return this.selectIds; } public Map<Integer, String> getTestMap() { return this.testMap; } public String getRemark() { return this.remark; } }
package com.demo.web.controllers; import java.util.Arrays; import java.util.HashMap; 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.demo.web.models.TagsModel; @Controller @RequestMapping(value = "/tags") public class TagsController { @RequestMapping(value = "/test", method = { RequestMethod.GET }) public String test(Model model) { if (!model.containsAttribute("contentModel")) { TagsModel tagsModel = new TagsModel(); tagsModel.setUsername("aaa"); tagsModel.setPassword("bbb"); tagsModel.setTestBoolean(true); tagsModel.setSelectArray(new String[] { "arrayItem 路人甲" }); tagsModel.setTestArray(new String[] { "arrayItem 路人甲", "arrayItem 路人乙", "arrayItem 路人丙" }); tagsModel.setRadiobuttonId(1); tagsModel.setSelectId(2); tagsModel.setSelectIds(Arrays.asList(1, 2)); Map<Integer, String> map = new HashMap<Integer, String>(); map.put(1, "mapItem 路人甲"); map.put(2, "mapItem 路人乙"); map.put(3, "mapItem 路人丙"); tagsModel.setTestMap(map); tagsModel.setRemark("備註..."); model.addAttribute("contentModel", tagsModel); } return "tagstest"; } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form:form modelAttribute="contentModel" method="post"> input 標籤:<form:input path="username" /> <br /> password 標籤:<form:password path="password" /> <br /> 綁定boolean的checkbox 標籤:<br /> <form:checkbox path="testBoolean" /> <br /> 綁定Array的checkbox 標籤:<br /> <form:checkbox path="testArray" value="arrayItem 路人甲" />arrayItem 路人甲 <form:checkbox path="testArray" value="arrayItem 路人乙" />arrayItem 路人乙 <form:checkbox path="testArray" value="arrayItem 路人丙" />arrayItem 路人丙 <form:checkbox path="testArray" value="arrayItem 路人丁" />arrayItem 路人丁<br /> 綁定Array的checkboxs 標籤:<br /> <form:checkboxes path="selectArray" items="${contentModel.testArray}" /> <br /> 綁定Map的checkboxs 標籤:<br /> <form:checkboxes path="selectIds" items="${contentModel.testMap}" /> <br /> 綁定Integer的radiobutton 標籤:<br /> <form:radiobutton path="radiobuttonId" value="0" />0 <form:radiobutton path="radiobuttonId" value="1" />1 <form:radiobutton path="radiobuttonId" value="2" />2<br /> 綁定Map的radiobuttons 標籤:<br /> <form:radiobuttons path="selectId" items="${contentModel.testMap}" /> <br /> 綁定Map的select 標籤:<br /> <form:select path="selectId" items="${contentModel.testMap}" /> <br /> 不綁定items數據直接在form:option添加的select 標籤:<br /> <form:select path="selectId"> <option>請選擇人員</option> <form:option value="1">路人甲</form:option> <form:option value="2">路人乙</form:option> <form:option value="3">路人丙</form:option> </form:select> <br /> 不綁定items數據直接在html的option添加的select 標籤:<br /> <form:select path="selectId"> <option>請選擇人員</option> <option value="1">路人甲</option> <option value="2">路人乙</option> <option value="3">路人丙</option> </form:select> <br /> 用form:option綁定items的select 標籤:<br /> <form:select path="selectId"> <option />請選擇人員 <form:options items="${contentModel.testMap}" /> </form:select> <br /> textarea 標籤: <form:textarea path="remark" /> <br /> <input type="submit" value="Submit" /> </form:form> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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>Insert title here</title> </head> <body> <a href="tags/test">Test tags</a> </body> </html>