使用< form:select>用地图标记 [英] Use <form:select> tag with a map

查看:159
本文介绍了使用< form:select>用地图标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将地图内的数据映射到标签?
我的代码中有一张地图 Map< String,Integer>
是否有办法将选项标签映射到映射中的 String ,将 Integer 映射到选项值?

Is there a way to map the data inside a map to tag? I have a map Map<String, Integer> in my code. Is there a way to map the option labels to the String in the map and the Integer to the option values?

推荐答案

< form:options> 标签支持使用项目属性即可开箱即用。你可以做这样的事情:

The <form:options> tag supports what you want right out of the box, using the items attribute. You can do something like this:

LinkedHashMap<Integer, String> states = new LinkedHashMap<Integer, String>();
states.put(1, "Alabama");
states.put(2, "Alaska");
states.put(3, "Arizona");
states.put(4, "Arkansas");
states.put(5, "California");

等等。然后在你的表单中:

And so on. Then in your form:

<form:select path="state">
    <form:options items="${states}" />
</form:select>

这将呈现如下所示:

<select name="state">
    <option value="1">Alabama</option>
    <option value="2">Alaska</option>
    <option value="3">Arizona</option>
    <option value="4">Arkansas</option>
    <option value="5">California</option>
</select>

这篇关于使用&lt; form:select&gt;用地图标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆