SpringMVC表單標籤(5)

1.8 password標籤

password標籤將會被渲染爲一個typepassword的普通HTML input標籤。

1.9 select標籤

select標籤將會被渲染爲一個普通的HTML select標籤。這裏還拿前面的user最喜歡的球類運動來做示例,有如下這樣一個處理器方法和對應的視圖頁面:

Java代碼 

  1. @RequestMapping(value="form", method=RequestMethod.GET)
  2. public String formTag(Map<String, Object> map) {
  3. User user = new User();
  4. user.setFavoriteBall(4);//設置我最喜愛的球類運動是4羽毛球
  5. Map<Integer, String> ballMap = new HashMap<Integer, String>();
  6. ballMap.put(1, "籃球");
  7. ballMap.put(2, "足球");
  8. ballMap.put(3, "乒乓球");
  9. ballMap.put(4, "羽毛球");
  10. ballMap.put(5, "排球");
  11. map.put("user", user);
  12. map.put("ballMap", ballMap);
  13. return"formTag/form";
  14. }

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

    }

Jsp代碼 

  1. <form:form action="formTag/form.do" method="post" commandName="user">
  2. <table>
  3. <tr>
  4. <td>最喜歡的運動:</td>
  5. <td>
  6. <form:select path="favoriteBall" items="${ballMap}"/>
  7. </td>
  8. </tr>
  9. <tr>
  10. <td colspan="2"><input type="submit" value="提交"/></td>
  11. </tr>
  12. </table>
  13. </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標籤指定了一個數據源,並且綁定了表單對象userfavoriteBall屬性。Items屬性是用於指定當前select的所有可選項的,但是它對於select標籤而言不是必須的,因爲我們還可以手動的在select標籤中間加上option標籤來指定select可選的optionSelect標籤支持的items屬性的數據類型可以是ArrayCollectionMap,當數據類型爲ArrayCollection時且其中的元素爲一個POJO時,我們可以通過屬性itemLabelitemValue來指定將用於呈現的option LabelValue,其他情況下ArrayCollection數據源中的元素將既作爲可選項optionvalue又作爲它的Label。當items的數據類型爲Map時,Mapkey將作爲可選項optionvalue,而Mapvalue將作爲optionLabel標籤。

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