Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help on c:forEach

 
Sam Drake
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you ask a more specific question, or show us an example where you want to do this?
 
Sam Drake
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
Thats quite a lot!!! And Thanks a lot!!!
Sam
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic