How to add index to ForEachTag (c:forEach as Java)?

J

javaguy

I'm wishing to write JSP pages that have varying background colors for
each line in the list. A simplistic way of doing this is:

<c:forEach items="${myorders}" var="orders" varStatus="loop">
<tr height="20"
<c:if test="${loop.index % 2 eq 0}">bgcolor='#BBBBBB'</c:if> >
<td>Some data</td>
</tr>
</c:forEach>

I was trying to extend the implementing Java class ForEachTag like the
example in the book Struts Recipies (2005, Franciscus, Gurovich,
Manning Publications, Greenwich CT). My version of their IteratorTag
would look like this:

<my:forEachCycle items="${myorders}" var="orders" varStatus="loop"
cycleId="cy" cycleItems="#FF0000 #00FF00 #0000FF">
<tr height="20" bgcolor='<c:set value="${cy}"/>' >
<td>Some data</td>
</tr>
</my:forEachCycle>

The difference between IteratorTag and ForEachTag is that IteratorTag
has a built-in getIndex() method I can use to know where in the list I
am. It also lets me cycle through my list (index is getIndex() %
list_length). I've looked through Google (and deja) for documentation
of org.apache.taglibs.standard.tag.el.core.ForEachTag.java and found
nothing. The ForEachTag descriptions I have found were all for other
implementations (Coocon, etc.) and not applicible here. It is more
frustrating because the forEach tag has ready access to "varStatus"!

So, How do I get an index from within a subclass of ForEachTag.java?

Thanks,
Jerome Mrozak.
 
J

javaguy

Solved my own problem. The ForEachTag class has a getLoopStatus()
method that returns a LoopTagStatus reference. That reference has
methods getCount() (1,2,3,...) and a getIndex() (0,1,2,...) that tell
me where in the loop I'm at.

Didn't get the answer at all from newsgroups or Google. There are a
lot of intances of ForEachTag, but they are all stuff from the "jelly"
versions, or home-grown versions. I got the answer from the NetBeans
editor, which did reflection into the class file(s). Didn't think I'd
ever find a use for an IDE! I dislike their footprints and don't seem
to need all the functionality they offer. Just give me a powerful
editor...except this time. I suppose you could get this answer from
Eclipse (being PC here :) ) but I like that IDE even less.

Jerome.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top