File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

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(rs.next()) {
Item_Desc item_desc = new Item_Desc();
item_desc.setItem_id(rs.getString(1));
item_desc.setItem(rs.getString(2));
item_desc.setsubCat(rs.getString(3));
item_desc.setManufacturer(rs.getString(4));
item_desc.setDescription(rs.getString(5));
list.add(item_desc);
} return list;
Iterating it using:
<logic:iterate name="item_descList"
id="item_desc"
scope="request"
type="com.ops.entity.Item_Desc">
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.
<subcat1>
<bean1 properties (has subcat1)>
<bean2 properties (has subcat1)>
<subcat2>
<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.


Regards,<p>Ols
Geeta Ramasami
Ranch Hand

Joined: Mar 05, 2003
Posts: 72
Hi,
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.
Regards
Geeta
[ 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(); %>
</logic:iterate>
</table>
Hope this is useful
Cheers
Geeta
Oliver Moore
Ranch Hand

Joined: Mar 04, 2003
Posts: 44
Brilliant, that's much better, thanks...
Ols
 
jQuery in Action, 2nd edition
 
subject: Iterate Array List
 
Similar Threads
Does Java, sometimes, do automatic type conversion?
InitializeInjectedBeanAtStartup
BeanUtils and null-properties.
A bean in each ear .....
list shuttle exception