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 Help on c:forEach Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Help on c:forEach" Watch "Help on c:forEach" New topic
Author

Help on c:forEach

Sam Drake
Ranch Hand

Joined: Jun 05, 2003
Posts: 33
Hi,
I'm facing a problem in which I have an ArrayList of objects. Can you please explain the syntax used to iterate through the Array List using <c:forEach..>
Thanks and regards,
Sam
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Can you ask a more specific question, or show us an example where you want to do this?


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Sam Drake
Ranch Hand

Joined: Jun 05, 2003
Posts: 33
Ok..lets say that I have some array of objects called APTJavaBeans in an arraylist AList. Please refer the code below -
ArrayList AList = new ArrayList();
APTJavaBeans[] beans = new APTJavaBeans[3];
...
..
//The array is populated with the bean objects
//initialized or otherwise
//now
AList.add(beans);
Now I have an array of beans kept in the session context.
How do I iterate through this using <c:forEach..> tag in my JSP?
Hope the qn is clear.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
I admit, the Docs on JSTL are still weak.
You are in luck on one point. CHAPTER 5 - Controlling flow with loops of Shawn Bayern's book "JSTL in Action" is on line. For a quick run down, ForEach has the following attributes:
items - the actual collection that will be iterated over
begin - the index to start the iterator at.
end - the index to end at
step - step the iterator takes
var - the name of the variable that will hold the item returned from each iteration.
varStatus - ?? hum I need to do somemore digging on this one.


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
In your code fragment, you have an ArrayList, one of whose elements is an array. Is that what you meant to do?
Here's some code I recently wrote:

Does that help at all?
[ August 14, 2003: Message edited by: Ron Newman ]
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
AHA!
Some digging through the source code and.....
varStatus sets the name of the variable that you can use to access through EL to get to the LoopTagStatus Object within the Tag. Provides the following methods for you to check.
getBegin() - Returns the value of the 'begin' attribute for the associated tag, or null if no 'begin' attribute was specified.
getCount() - Retrieves the "count" of the current round of the iteration.
getCurrent() - Retrieves the current item in the iteration.
getEnd() - Returns the value of the 'end' attribute for the associated tag, or null if no 'end' attribute was specified.
getIndex() - Retrieves the index of the current round of the iteration.
getStep() - Returns the value of the 'step' attribute for the associated tag, or null if no 'step' attribute was specified.
isFirst() - Returns information about whether the current round of the iteration is the first one.
isLast() - Returns information about whether the current round of the iteration is the last one.
This allows you to do some decissions for such things as paging, Display something different for the first/ last item only etc.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
varStatus is a bean with some getter functions that return various information about the ongoing iteration.
See section 6.4 of the JSTL 1.0 spec, "Interface LoopTagStatus".
Sam Drake
Ranch Hand

Joined: Jun 05, 2003
Posts: 33
Hi guys,
Thats quite a lot!!! And Thanks a lot!!!
Sam
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help on c:forEach
 
Similar Threads
EL
Looping problem in the display.
using c:forEach to iterate over Map<String, ArrayList<Hashmap>>
How to display data from an arraylist of more than one bean on jsp page using scripting or jsp tags?
iterate an object in struts-2