You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-user@tomcat.apache.org by Ben Anderson <be...@benanderson.us> on 2004/09/22 21:40:41 UTC

last element in collection

Hi,
Is there a way for me to tell when I'm in the last iteration of a forEach?

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<jsp:useBean id="myList" class="java.util.ArrayList"/>
<%
  myList.add("1");
  myList.add("2");
  myList.add("3");
%>
<c:forEach var="entry" items="${myList}">
<!--  This is the last time through
      <c:if test="">
        do something special
      </c:if>
-->
  <c:out value="${entry}"/>
  <br />
</c:forEach>

I know it'd be better if my logic didn't need to know this, but I'm just
wondering if there's a way to do it.  I thought maybe I could get the size
and stick it in a var like
<c:set var="size" value="myList.xxx"/>
<c:forEach var="entry" items="${myList}" end="${size}>

but the size() method on java.util.Collection is just that, not getSize().
Any ideas?
Thanks,
Ben

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: last element in collection

Posted by Martin Cooper <mf...@gmail.com>.
On Wed, 22 Sep 2004 15:43:09 -0400, Kris Schneider <kr...@dotech.com> wrote:
> <c:forEach var="entry" status="status" items="${myList}">

That would be 'varStatus' rather than 'status'. ;-)

--
Martin Cooper


>  ...
>  <c:if test="${status.last}">
>    ...
>  </c:if>
> </c:forEach>
> 
> Quoting Ben Anderson <be...@benanderson.us>:
> 
> > Hi,
> > Is there a way for me to tell when I'm in the last iteration of a forEach?
> >
> > <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
> > <jsp:useBean id="myList" class="java.util.ArrayList"/>
> > <%
> >   myList.add("1");
> >   myList.add("2");
> >   myList.add("3");
> > %>
> > <c:forEach var="entry" items="${myList}">
> > <!--  This is the last time through
> >       <c:if test="">
> >         do something special
> >       </c:if>
> > -->
> >   <c:out value="${entry}"/>
> >   <br />
> > </c:forEach>
> >
> > I know it'd be better if my logic didn't need to know this, but I'm just
> > wondering if there's a way to do it.  I thought maybe I could get the size
> > and stick it in a var like
> > <c:set var="size" value="myList.xxx"/>
> > <c:forEach var="entry" items="${myList}" end="${size}>
> >
> > but the size() method on java.util.Collection is just that, not getSize().
> > Any ideas?
> > Thanks,
> > Ben
> 
> -- 
> Kris Schneider <ma...@dotech.com>
> D.O.Tech       <http://www.dotech.com/>
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: taglibs-user-help@jakarta.apache.org
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: last element in collection

Posted by Kris Schneider <kr...@dotech.com>.
Sorry, the "status" attribute should be "varStatus"...

Quoting Kris Schneider <kr...@dotech.com>:

> <c:forEach var="entry" status="status" items="${myList}">
>   ...
>   <c:if test="${status.last}">
>     ...
>   </c:if>
> </c:forEach>
> 
> Quoting Ben Anderson <be...@benanderson.us>:
> 
> > Hi,
> > Is there a way for me to tell when I'm in the last iteration of a forEach?
> > 
> > <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
> > <jsp:useBean id="myList" class="java.util.ArrayList"/>
> > <%
> >   myList.add("1");
> >   myList.add("2");
> >   myList.add("3");
> > %>
> > <c:forEach var="entry" items="${myList}">
> > <!--  This is the last time through
> >       <c:if test="">
> >         do something special
> >       </c:if>
> > -->
> >   <c:out value="${entry}"/>
> >   <br />
> > </c:forEach>
> > 
> > I know it'd be better if my logic didn't need to know this, but I'm just
> > wondering if there's a way to do it.  I thought maybe I could get the size
> > and stick it in a var like
> > <c:set var="size" value="myList.xxx"/>
> > <c:forEach var="entry" items="${myList}" end="${size}>
> > 
> > but the size() method on java.util.Collection is just that, not getSize().
> > Any ideas?
> > Thanks,
> > Ben
> 
> -- 
> Kris Schneider <ma...@dotech.com>
> D.O.Tech       <http://www.dotech.com/>

-- 
Kris Schneider <ma...@dotech.com>
D.O.Tech       <http://www.dotech.com/>

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: last element in collection

Posted by Kris Schneider <kr...@dotech.com>.
<c:forEach var="entry" status="status" items="${myList}">
  ...
  <c:if test="${status.last}">
    ...
  </c:if>
</c:forEach>

Quoting Ben Anderson <be...@benanderson.us>:

> Hi,
> Is there a way for me to tell when I'm in the last iteration of a forEach?
> 
> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
> <jsp:useBean id="myList" class="java.util.ArrayList"/>
> <%
>   myList.add("1");
>   myList.add("2");
>   myList.add("3");
> %>
> <c:forEach var="entry" items="${myList}">
> <!--  This is the last time through
>       <c:if test="">
>         do something special
>       </c:if>
> -->
>   <c:out value="${entry}"/>
>   <br />
> </c:forEach>
> 
> I know it'd be better if my logic didn't need to know this, but I'm just
> wondering if there's a way to do it.  I thought maybe I could get the size
> and stick it in a var like
> <c:set var="size" value="myList.xxx"/>
> <c:forEach var="entry" items="${myList}" end="${size}>
> 
> but the size() method on java.util.Collection is just that, not getSize().
> Any ideas?
> Thanks,
> Ben

-- 
Kris Schneider <ma...@dotech.com>
D.O.Tech       <http://www.dotech.com/>

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org