Drop-Down in Struts 2

后端 未结 2 866
失恋的感觉
失恋的感觉 2021-01-26 12:22

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

相关标签:
2条回答
  • 2021-01-26 13:03

    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.

    0 讨论(0)
  • 2021-01-26 13:15

    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 讨论(0)
提交回复
热议问题