This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes JSP and the fly likes Iterate Array List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » JSP
Bookmark "Iterate Array List" Watch "Iterate Array List" New topic

Iterate Array List

Oliver Moore
Ranch Hand

Joined: Mar 04, 2003
Posts: 44
I am currently passing an array list of beans created with the following code to a jsp:
while( {
Item_Desc item_desc = new Item_Desc();
} return list;
Iterating it using:
<logic:iterate name="item_descList"
gives me a table with columns for each property of the bean "item_desc", however many of the items in the list have the same subcat.
Could someone sugest a way to write the subcat once, then rows consisting of the other bean properties until the subcat changes, at which point the new subcat is changed again. i.e.
<bean1 properties (has subcat1)>
<bean2 properties (has subcat1)>
<bean1 properties (has subcat2)>
<bean2 properties (has subcat2)>
The list is already ordered by subcat from the sql querey which populates the list.
Any help is much aprieciated.

Geeta Ramasami
Ranch Hand

Joined: Mar 05, 2003
Posts: 72
If i have understood the problem correctly,for displaying each replicant SubCat only once u can build a logic which checks the previous SubCat value with the current Subcat value by setting the previous SubCat value to the Current value at the end of each iteration of the loop.If the previous value and the current value are different then display the SubCat value.Is this the one u r looking for or something else.
[ March 10, 2003: Message edited by: Geeta Ramasami ]
[ March 10, 2003: Message edited by: Geeta Ramasami ]
Oliver Moore
Ranch Hand

Joined: Mar 04, 2003
Posts: 44
Thanks Geeta, that idea worked fine...

I could cut down some of the code if I could do a "notequals" for the strings, but I can't seem to find a way of doing that...
Geeta Ramasami
Ranch Hand

Joined: Mar 05, 2003
Posts: 72
Hi Oliver,
You can check for notEquals and optimise the code as follows:
<% String prevsubCat = "checking"; %>
<logic:iterate name="item_descList" id="item_desc" scope="request" type="com.ops.entity.Item_Desc">
<% String currentsubCat = item_desc.getsubCat();
if (prevCat != null && !prevsubCat.equals(currentsubCat))
{ %>
<td colspan="4"> <b><bean:write name="item_desc" property="subCat"/></b> </td>
<td> <bean:write name="item_desc" property="item_id"/> </td>
<td> <bean:write name="item_desc" property="item"/> </td>
<td> <bean:write name="item_desc" property="Description"/> </td>
<td> <bean:write name="item_desc" property="manufacturer"/> </td>
<% prevsubCat = item_desc.getsubCat(); %>
Hope this is useful
Oliver Moore
Ranch Hand

Joined: Mar 04, 2003
Posts: 44
Brilliant, that's much better, thanks...
Have you checked out Aspose?
subject: Iterate Array List
It's not a secret anymore!