Search code examples
jspstruts2iteratorognlstruts-tags

Struts2 - property tag inside another property


I have an iterator which iterates over the list of class object.

<s:iterator value="list" status="status" id="id">
    <tr data-id="<s:property value="#status.count"/>" 
             id="<s:property value="#status.count"/>" 
          title="insert">

        <s:iterator value="value" status="status">

            <s:property value="%{<s:property/>}"/>

            <td data-field="<s:property value="key"/>_<s:property/>">
                <s:text name="%{<s:property/>}"/></td>
        </s:iterator>
    </tr>
</s:iterator>

Is it possible to get a property value from another property tag in Struts2 ?


Solution

  • There are several ways to do this.

    The simplest is to push an instance of the currently iterated element onto the Value Stack. You are doing it for the first iterator with id (that is deprecated: var should be used instead), you need to do it for the second one instead.

    Also consider changing name to the IteratorStatus instance, so that you can (if needed) access the outer status from within the inner iteration.

    <s:iterator value="list" status="status1" var="currentElement1">
        <tr data-id="<s:property value="#status1.count"/>" 
                 id="<s:property value="#status1.count"/>" 
              title="insert">
    
            <s:iterator value="#currentElement1.value" status="status2" var="currentElement2">
    
                <s:property value="%{#currentElement2}"/>
    
                <td data-field="<s:property value="#currentElement1.key"/>_<s:property/>">
                    <s:text name="%{#currentElement2}"/></td>
            </s:iterator>
        </tr>
    </s:iterator>
    

    Also consider using more meaningful names... calling a List list and an object value might be confusing with the growth of the application.