2

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?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Subash L
  • 215
  • 1
  • 3
  • 11

2 Answers2

3

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:

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Yes, I found a constructor, but its in parent class. I have given no arg constructor, so it should work right? But still not working – Subash L Jul 02 '15 at 15:05
  • I've removed that part of the answer because it is meaningful when posting the data from JSP to action, but it is ininfluent when reading data from action to JSP (that is your case). I suspect you have some typo somewhere. Can you post the real code or it is sensitive data ? – Andrea Ligios Jul 02 '15 at 15:11
  • Yes it is sensitive data. I checked for typo, but I don't think that's the issue. – Subash L Jul 02 '15 at 15:13
  • Have you read the update ? Can you post at least the real children and childrenName variables name ? – Andrea Ligios Jul 02 '15 at 15:23
  • considering "children" is a list of Children don't you have to tell struts what instance of "children" it's looking at in the Children list? – gcalex5 Jul 02 '15 at 15:29
  • 1
    @gcalex5 You are iterating *all* the instances of Chidren in the List. `` is an iterator, and takes a List as source. What you are saying would be necessary to print a child with ``, for example – Andrea Ligios Jul 02 '15 at 15:32
  • 1
    Yes the problem is because of the getter setter, as you mentioned in EDIT – Subash L Jul 02 '15 at 15:32
  • Great. I like the ones solved without even seeing the real code :P – Andrea Ligios Jul 02 '15 at 15:33
  • 1
    I been searching for a solution for 2 hours. Thanks Andrea. You are awesome. – Subash L Jul 02 '15 at 15:37
  • 1
    This example helped me as well. – Thor Hovden Apr 25 '17 at 05:57
0

Since your parent object has a list of children objects you need to tell Struts what instance of the children you want so you would need to change the children iterator to something like this

s:iterator id="children" value="children" var="children_list_var">
    <s:property value="#children_list_var.childrenName"/>
</s:iterator>

Take a look at this link to get a better sense of what's going on https://struts.apache.org/docs/ognl-basics.html

Edit: assuming everything is properly mapped then I would look at your getter/setter methods and make sure it's hitting the proper one. Another thing you can try is add a variable to your iterator to make sure you are grabbing the right instance. with something like my above code example I changed.

What that does is grab the current children object in the list and return the children name variable. I think it's a round about way of going about it but that should get what you want if the other answer isn't working

gcalex5
  • 1,091
  • 2
  • 13
  • 23