Search code examples
javajspstruts2iteratorognl

Struts Iterator Inside another Iterator


I am having a list, inside that I have another list. I tried to display in jsp through struts, but I couldn't. This is my code

<s:iterator id="parent" value="parent" status="stat">
    <s:property value="parentName"/>
    <s:iterator id="children" value="children" status="stat">
        <s:property value="childrenName"/>
    </s:iterator>
</s:iterator>

It is displaying parent name, but not the child name. I tried to display the children name before going to jsp, its is logging in java. I tried to search this solution, but the answers didn't solve my problem. This is my parent class.

class Parent{
  private ArrayList<Children> children;
  private String parentName;

  // Getter setter
}

This is children class

class Children{
  private String childrenName;

  // Getter setter
}

What is wrong with my code?


Solution

  • It should work.

    Just a couple of little corrections and suggestion:

    • id is deprecated, use var. If your case is like the one in the example, it is not even needed, then just avoid putting it at all:
    • two status with the same name is not good. Is source of confusion for both you and Mr. OGNL. Change the names, or avoid using one or both of them.
    • it is better to use the interface, not the implementation, to declare your objects:

      private List<Children> children;
      

    Then try with this:

    <s:iterator value="parent">
        <s:property value="parentName"/>
        <s:iterator value="children">
            <s:property value="childrenName"/>
        </s:iterator>
    </s:iterator>
    

    EDIT: