Search code examples
jspstruts2optgroupstruts1struts-tags

<s:select> with optgroup in Struts2


I am working on a task of migrating existing Struts1 application to Struts2. I am stuck in the problem. Within an <s:select> I need to categories the options with <optgroup>. The following code would do this in Struts1. How can I achieve this with Struts2?

<html:select id="foodList" property="foodItemId" onclick="selectRadio('0')">
    <c:if test="${not empty foodList1}">
        <optgroup label="Risk Analysis Report Sets">
            <c:forEach var="item" items="${foodList1}">
                <option value="${item.value}">${item.label}</option>
            </c:forEach>
        </optgroup>
    </c:if>
    <c:if test="${not empty foodList2}">
        <optgroup label="NMX Report Sets">
    <c:forEach var="item" items="${foodList2}">
                <option value="${item.value}">${item.label}</option>
            </c:forEach>
        </optgroup>
    </c:if>
    <c:if test="${not empty foodList3}">
        <optgroup label="Standard Report Sets">
            <c:forEach var="item" items="${list3}">
                <option value="${item.value}">${item.label}</option>
            </c:forEach>
        </optgroup>
    </c:if>
</html:select>

Solution

  • <s:optgroup/> is your friend here.

    From Struts2 JavaDoc:

    org.apache.struts2.components

    Class OptGroup

    Create a optgroup component which needs to resides within a select tag.

    This component is to be used within a Select component.

    <s:select label = "My Selection"
               name = "mySelection"
              value = "%{'POPEYE'}"
               list = "%{#{'SUPERMAN':'Superman', 'SPIDERMAN':'spiderman'}}">
    
        <s:optgroup label = "Adult"
                     list = "%{#{'SOUTH_PARK':'South Park'}}" />
        <s:optgroup label = "Japanese"
                     list = "%{#{'POKEMON':'pokemon','DIGIMON':'digimon'}}" />
    </s:select>