I have a map with key-value pairs. I want to display the key on a drop-down and value to be sent to server for identifying what is selected.
I am using Struts2. I tried putting the map in select tag list, however it shows the values in the dropdown.
Use listKey
and listValue
attributes of <s:select>
tag to get properties from list of objects.
In order to swap keys and values in map put value in listKey
and key in listValue
.
<s:select list="someMap" listKey="value" listValue="key"/>
As far as you know the dropdown in Struts2 can be used with a list or any other collection like a map. It uses OGNL to retrieve the values for the options text and values.
The difference from the list is a map is converted via entrySet()
and iterated to get Map.Entry
element for the select option. This object you can use to map a key and value for your dropdown. It has getKey()
and getValue()
methods that is useful to OGNL to populate a dropdown. The first is used to populate a value attribute, that is submitted to the action if it's selected, and a second fills an option text.
If your dropdown shows values, then you map them wrong. The values should be mapped as a keys of the Map
, and display texts as values.