This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes Nested ArrayLists using logic:iterate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Nested ArrayLists using logic:iterate " Watch "Nested ArrayLists using logic:iterate " New topic
Author

Nested ArrayLists using logic:iterate

K. E. Kerks
Greenhorn

Joined: Jan 04, 2005
Posts: 3
I've been struggling with this issue all day, and I need some help. I am in no way a Struts expert and have relied on a peer and a book to get me to where I am. Please help!

I have an attribute on session (claimants), which is an ArrayList of a bean which contains several properties, one of which is another ArrayList (causes). I need to loop through claimants, rendering the instance properties (like claimant name) on a line, followed by all causes (0 to many) under the claimant name... something like this ...

JOHN DOE (property name on claim index #0)
Cause #1 (cause name on cause index #0)
Cause #2 (cause name on cause index #1)
Cause #3 (cause name on cause index #2)

JANE SMITH (property name on claim index #0)
Cause #1 (cause name on cause index #0)
Cause #2 (cause name on cause index #1)

I have this code in my jsp ...

<logic:iterate id="claimant" name="claimants" indexId="i" type="ClaimantBean" >
<TR>
<TD><IMG expand.gif"></TD>
<TD><bean:write name="claimant" property="claimantName" /></TD>
</TR>
<logic:iterate id="item" name="claimant" property="causes" type="CauseOfLossBean" />
<TR>
<TABLE border="0" cellpadding="0" cellspacing="0" width="75%">
<TBODY>
<TR class="norm8" valign="bottom">
<TD><bean:write name="item" property="lossDesc" /></TD>
</TR>
</TBODY>
</TABLE>
</TR>
</logic:iterate>
</logic:iterate>

What seems to be happening is that when the second iteration renders only one row when I know there are multiples.

Am I doing this right? I cannot use el tags, and have to rely upon Struts tag lib. I'm not even sure that's the right terminology.

Thanks in advance!
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
"STL Java Novice",

Welcome to JavaRanch. We don't have many rules here, but we do have a naming policy which we strictly enforce. Please re-read this policy and edit your display name in order to comply. Thanks in advance, and we look forward to seeing you around the Ranch.

As far as your question, you want to take a look at the Struts nested taglib. Check out this site and read the sections titled [NeXt] and PilotLight for tutorials on using this taglib.
K. E. Kerks
Greenhorn

Joined: Jan 04, 2005
Posts: 3
Thanks. I'll look at the references you provided and see if I can figure it out.
sreenath reddy
Ranch Hand

Joined: Sep 21, 2003
Posts: 415
Hi

i hope this piece of code will help u in resolving the things

<logic:iterate id="claimant" name="claimants" indexId="index">
<logic:iterate id="item" name="claimant" property="causes" offset="index" length="1" type="CauseOfLossBean">
<bean:write name="item"/>
</logic:iterate>
</logic:iterate>


the addition of offset="index" and length="1" in the second iteartion will help u
K. E. Kerks
Greenhorn

Joined: Jan 04, 2005
Posts: 3
Thanks for your suggestion. However, it did not work for me.

My solution was to change the inner logic:iterate to a nested:iterate, which then successfully looped through the causes array.

Thanks, again.
 
Consider Paul's rocket mass heater.
 
subject: Nested ArrayLists using logic:iterate
 
Similar Threads
ActionForm with indexed properties
Set bean property values
Retrieving data using iterate and eliminating duplicate values
Accessing elements from an arraylist
formatting problem