struts2populatestruts-tagsdouble-select

How can I make a dynamic dropdowns in a Struts 2 using a Map object in an Action class?


In an Action class I have Map<String, List<String>> countryMap.

Let's say the key of countryMap are the letters of the alphabet and the value are the Country objects that starts with the corresponding key.

For example:

Key: A

Value: List contains Australia, Angola, Algeria, etc.

I want to make two dynamic dropdowns in Struts 2.

Dropdown 1 looks something like:

 <select>
     <option value="A">A</option>
     <option value="B">B</option>
 </select>

In dropdown two will show the value depending the key on dropwdown 1. For example, I chose A in dropdown 1, the dropwdown will populate the values.

Dropdown 2 looks something like:

<select>
    <option value="Australia">Australia</option>
    <option value="Angola">Angola</option>
</select>

I'm thinking of using <s:select> tag of Struts 2. I don't know how to start. I'm not well-versed with these functionalities. With this complicated code, I might need JavaScript involvement. I'm having a hard time starting developing the code.


Solution

  • Making two tags to communicate with each other on the same page is impossible without javascript. Fortunately there's Struts2 doubleselect tag that can be use in that scenario.

    It renders two HTML select elements with second one changing displayed values depending on selected entry of first one.

    <s:doubleselect label="Select country/state" name="country" list="{'country1','other'}" doubleName="state" doubleList="top == 'country1' ? {'state1', 'state2'} : {'state3', 'state4'}" />
    

    For the real example see Struts 2 <s:doubleselect> example.