Search code examples
javahtmljspstruts2ognl

How to choose a value of a HashMap in select Struts2?


I ran into a problem with Struts2. I have a HashMap<String, Integer> that I use in a form with a select to choose name and corresponding id to that name.

HashMap iterates and populates my <select>list with names. I only need an id to pick. How do I pick an id/value instead of a name/key?

companyMap is a HashMap, where <String, Integer> or key, value.

companyName is a String.

<s:select label="Pick the company name" 
    headerKey="-1" headerValue="Select Company name"
    list="companyMap.keys" 
    name = "companyName"
/>

Solution

  • If the list is a Map (key, value), the Map key will become the option 'value' parameter and the Map value will become the option body.

    But you need to change this rule

    <s:select label="Pick the company name" 
        headerKey="-1" headerValue="Select Company name"
        list="%{companyMap.entrySet()}" 
        name = "companyId"
        listKey="value"
        listValue="key"
    />