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

Discussion in 'Java' started by javaguy@sbcglobal.net, Apr 4, 2005.

  1. Guest

    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.
    , Apr 4, 2005
    #1
    1. Advertising

  2. Guest

    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.
    , Apr 5, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mike

    find index value of foreach

    mike, Nov 18, 2005, in forum: ASP .Net
    Replies:
    8
    Views:
    735
    Patrick.O.Ige
    Nov 18, 2005
  2. hugo
    Replies:
    15
    Views:
    3,658
  3. karthikeyavenkat
    Replies:
    2
    Views:
    584
    Bryce
    Mar 17, 2005
  4. Tim Shoppa
    Replies:
    45
    Views:
    493
    Darren Dunham
    Dec 23, 2003
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    285
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page