1.8 password標籤
password標籤將會被渲染爲一個type爲password的普通HTML input標籤。
1.9 select標籤
select標籤將會被渲染爲一個普通的HTML select標籤。這裏還拿前面的user最喜歡的球類運動來做示例,有如下這樣一個處理器方法和對應的視圖頁面:
- @RequestMapping(value="form", method=RequestMethod.GET)
- public String formTag(Map<String, Object> map) {
- User user = new User();
- user.setFavoriteBall(4);//設置我最喜愛的球類運動是4羽毛球
- Map<Integer, String> ballMap = new HashMap<Integer, String>();
- ballMap.put(1, "籃球");
- ballMap.put(2, "足球");
- ballMap.put(3, "乒乓球");
- ballMap.put(4, "羽毛球");
- ballMap.put(5, "排球");
- map.put("user", user);
- map.put("ballMap", ballMap);
- return"formTag/form";
- }
@RequestMapping(value="form", method=RequestMethod.GET)
public String formTag(Map<String, Object> map) {
User user = new User();
user.setFavoriteBall(4);//設置我最喜愛的球類運動是4羽毛球
Map<Integer, String> ballMap = new HashMap<Integer, String>();
ballMap.put(1, "籃球");
ballMap.put(2, "足球");
ballMap.put(3, "乒乓球");
ballMap.put(4, "羽毛球");
ballMap.put(5, "排球");
map.put("user", user);
map.put("ballMap", ballMap);
return "formTag/form";
}
- <form:form action="formTag/form.do" method="post" commandName="user">
- <table>
- <tr>
- <td>最喜歡的運動:</td>
- <td>
- <form:select path="favoriteBall" items="${ballMap}"/>
- </td>
- </tr>
- <tr>
- <td colspan="2"><input type="submit" value="提交"/></td>
- </tr>
- </table>
- </form:form>
<form:form action="formTag/form.do" method="post" commandName="user">
<table>
<tr>
<td>最喜歡的運動:</td>
<td>
<form:select path="favoriteBall" items="${ballMap}"/>
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"/></td>
</tr>
</table>
</form:form>
這個時候會渲染出如下結果:
從上面示例我們可以看出,我們通過items屬性給select標籤指定了一個數據源,並且綁定了表單對象user的favoriteBall屬性。Items屬性是用於指定當前select的所有可選項的,但是它對於select標籤而言不是必須的,因爲我們還可以手動的在select標籤中間加上option標籤來指定select可選的option。Select標籤支持的items屬性的數據類型可以是Array、Collection和Map,當數據類型爲Array或Collection時且其中的元素爲一個POJO時,我們可以通過屬性itemLabel和itemValue來指定將用於呈現的option Label和Value,其他情況下Array和Collection數據源中的元素將既作爲可選項option的value又作爲它的Label。當items的數據類型爲Map時,Map的key將作爲可選項option的value,而Map的value將作爲option的Label標籤。