如何在struts 2 select中显示不同的值
我想知道如何在 struts 2 select 中显示不同的值。 例如:我想将 Jan、Feb 作为月份,其中值应分别超过 1,2。
如果有人知道这一点请告诉我 谢谢。
I want to know how to display a different value in struts 2 select.
eg: i want to put Jan,Feb as month where the value should pass 1,2 respectively.
if anybody aware of this please let me know
thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅以下示例(来自 Struts 2.1.8 API 文档):
list 属性包含一个映射,其中键是将要发送的值,值是将要显示的值。
当然,月份是静态的,但您可以使用域对象列表或任何您需要的 bean。在这种情况下,应该存储列表,通常作为操作类的字段。然后您将引用列表或地图:
在这种情况下,您的操作将包含一个带有用户名及其 ID 的地图以及一个 getter:
getUsers()
。如果您的操作的 getUsers() 方法返回 User 对象列表,并且 User 类至少具有(让我们假设)
id
和username
字段,那么您将拥有指定用于传递值的字段以及用于在选择中显示的字段。这是通过 select 标签的listKey
和listValue
属性完成的:See the following example (from the Struts 2.1.8 API doc) :
The list attribute contains a map where the key is the value that will be sent and the value is the value that will be displayed.
Of course, months are static, but you could use a list of domain objects or whatever beans you need. In this case the list should be stored, typically as a field of your action class. Then you will refer to the list or map :
In this case your action will contain a map with the usernames and their ids and a getter for it :
getUsers()
.If the getUsers() method of your action returns a list of User objects, and the User class has at least (let's assume) the
id
andusername
fields, you will have to specify which field to use for value to be passed and which field to use for display in the select. This is done with thelistKey
andlistValue
attributes of the select tag :