Drop-Down in Struts 2

1.7k views Asked by At

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 <s:select> tags list, however it shows the values in the dropdown.

2

There are 2 answers

1
Aleksandr M On BEST ANSWER

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"/>
0
Roman C On

As far as you know the dropdown in Struts 2 can be used with List or any other collection like Map. It uses OGNL to retrieve the values for the options text and values.

The difference from the List is that the 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, which is submitted to the action if it's selected, and a second fills an option's 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.