Hi, I'm new to this. 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.
Hi, I'm new to this. 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.
See the following example (from the Struts 2.1.8 API doc) :
<s:select label="Months"
name="months"
headerKey="-1" headerValue="Select Month"
list="#{'01':'Jan', '02':'Feb', [...]}"
value="selectedMonth"
required="true"
/>
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 :
<s:select label="User"
name="users"
headerKey="-1" headerValue="Select User"
list="users"
value="selectedUser"
required="true"
/>
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
and username
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 the listKey
and listValue
attributes of the select tag :
<s:select label="User"
name="users"
headerKey="-1" headerValue="Select User"
list="users"
listKey="id"
listValue="username"
value="selectedUser"
required="true"
/>