javalistloopsstruts2ognl

Getting an object attributes while iterating through a List in Struts 2


I'm iterating through a List<SomeObject>, but I want only to print out certain attributes of SomeObject, depending on the attributes specified in another List. So far, I have got it. What I'm not sure, how to use the item in the List as the attribute of SomeObject that I want to print out.

<s:iterator value="theList" var="listItem" status="theListCount">
    <s:iterator value="listOfAttributes" var="attr" status="attrListCount">
        Attribute=<s:property value="#attr"/> Value=<s:property value='#listItem.[%{#attr}]'/>
    </s:iterator>
</s:iterator>

Solution

  • First of all #attr is a reserved keyword, so don't use it as a variable value. And use [] notation to reference property of the object.

    <s:iterator value="theList" var="listItem" status="theListCount">
      <s:iterator value="listOfAttributes" var="atrbt" status="attrListCount">
          Attribute=<s:property value="#atrbt"/> Value=<s:property value="#listItem[#atrbt]"/>
      </s:iterator>
    </s:iterator>