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标签。

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