Sabitha
Sabitha

Reputation: 283

Struts2 iterator to populate dropdown with different types

I have a Map whose key is a String and value may be either a String or another Map which depends on response returned from a Java file.

I need to populate that response in a drop-down using Struts2 iterator.

My requirement is, If the value is String, I need to use <option> tag. If the value is a Map I need to go for <optgroup>.

So, I need to check if the 'value' of that Map is String or Map and populate it correspondingly.

Upvotes: 2

Views: 1994

Answers (1)

Andrea Ligios
Andrea Ligios

Reputation: 50203

Just easy as it seems:

<select name="foo">

    <s:iterator value="myMapOfMaps" var="currentEntry">
        <s:if test="%{#currentEntry.value instanceof java.util.Map}">

            <optgroup label="<s:property value='%{#currentEntry.key}'/>">
                <s:iterator value="#currentEntry.value" var="innerEntry">

                    <option value="<s:property value='%{#innerEntry.key}'/>">
                        <s:property value='%{#innerEntry.value}' />
                    </option>

                </s:iterator>
            </optgroup>

        </s:if>
        <s:else>

            <option value="<s:property value='%{#currentEntry.key}'/>">
                <s:property value='%{#currentEntry.value}' />
            </option>

        </s:else>
    </s:iterator>

</select>

Upvotes: 2

Related Questions