The Iteration Tags

printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    

JSTL: JSP Standard Tag Library Kick Start
By Jeff Heaton

Table of Contents
Chapter 5.  Collections, Loops, and Iterators


JSTL supports two tags that are used to iterate over collections. For iteration over the collections just discussed, you use the <c:forEach> tag. As we stated earlier, the <c:forEach> tag can iterate over comma-delineated strings. It is also possible to iterate over strings that are delineated by characters other than the comma. To iterate over such strings, you must use the <c:forTokens> tag. In this section, we discuss the <c:forEach> tag. In the next section, we will discuss the <c:forTokens> tag.

The <c:forEach> Tag

The <c:forEach> tag is used to iterate over a collection or to iterate a fixed number of times. There are two forms of the <c:forEach> tag:

// Syntax 1: Iterate over a collection of objects <c:forEach [var="varName"] items="collection" [varStatus="varStatusName"] [begin="begin"] [end="end"] [step="step"]> body content </c:forEach> // Syntax 2: Iterate a fixed number of times <c:forEach [var="varName"] [varStatus="varStatusName"] begin="begin" end="end" [step="step"]> body content </c:forEach> 

In syntax 1, you must provide the <c:forEach> tag with a collection to iterate over. You specify this collection by using the items attribute. Each item in the collection will be referenced by the variable specified in the var attribute of the <c:forEach> tag. This loop will continue until all of the items in the collection have been processed.

Syntax 2 does not rely on collections to set the boundaries that it will loop through. This example specifies a beginning, ending, and count for the loop. The attribute begin specifies where the loop should begin. The attribute end specifies where the loop should end; the end is inclusive. How far to move through each iteration of the loop is determined by the step attribute. Using these three attributes allows you to emulate the behavior of Java's for loop.

One deficiency of the fixed-loop <c:forEach> tag is that it will not loop backward. While it may seem logical that the following loop would count backward from 10, this loop would generate an exception:

<c:forEach begin="10" end="1" var="i" step="-1">   <c:out value="${i}"/><br/> <c:/forEach> 

The <c:forEach> tag also includes the varStatus attribute, which enables you to obtain information about the iteration as it progresses. We discuss the varStatus attribute next.

WARNING

One limitation of the <c:forEach> tag is that the begin, end, and step attributes must be constant. It is invalid to specify another scoped variable for these values.


Loop Status

JSTL also gives you the ability to access information about the iteration as it proceeds. You accomplish this by using a varStatus variable. If you specify a varStatus variable, the iterator will fill it with a javax.servlet.jsp.jstl.core.LoopTagStatus object for each loop of the iterator. Because this is a standard Java object, you must use the RT expression language (explained in Chapter 4, "Using the Expression Language") to access it.

One common use of this object is to get access to the iteration number we are on. We can then specify a different action for even and odd loop numbers. The following code shows how this is done:

<c:forEach var="product" items="${products}" varStatus="status">   <jsp:useBean      type="javax.servlet.jsp.jstl.core.LoopTagStatus"/>     <c-rt:choose>       <c-rt:when test="<%=status.getCount()%2 ==0 %>">         even item<br/>       </c-rt:when>     <c-rt:otherwise>       odd item<br/>     </c-rt:otherwise>   </c-rt:choose> </c:forEach> 

As you can see, the varStatus object must be registered as a bean for it to be used. We do this with the following line:

<jsp:useBean  type="javax.servlet.jsp.jstl.core.LoopTagStatus"/> 

Registering this object allows you to access its properties. Our example uses the getCount() method. In addition to getCount(), you may wish to use some of the other methods we describe in the following sections.

The getCurrent() Method

The getCurrent() method has the following prototype:

public java.lang.Object getCurrent() 

This method returns the current item from the iteration. Calling this method does not advance the iteration. Calling the method repeatedly returns the same object until the iteration is advanced.

The getIndex() Method

The getIndex() method has the following prototype:

public int getIndex() 

This method returns the index of the current iteration. If the iteration is being performed over a subset of an underlying array, java.lang.Collection, or another type, the index returned is absolute. The index does not take into account the value of the begin, step, and end attributes. The index is that of the underlying Java collection and is 0-based.

The getCount() Method

The getCount() method has the following prototype:

public int getCount() 

This method returns the count of the current round of the iteration. The count is a relative, 1-based sequence number that identifies the round of iteration. For example, an iteration with begin = 6, end = 10, and step = 2 produces the counts 1, 2, and 3, in that order.

The isFirst() Method

The isFirst() method has the following prototype:

public boolean isFirst() 

This method returns true if this is the first round through the iteration. This method may return true even when the result of getIndex() is not equal to 0. This is because index specifies the absolute index of the current item based on the context of its underlying collection. However, it is always the case that a true result from isFirst() ensured that the value returned by getCount() will be 1. This difference in indexes is called subsetting.

The isLast() Method

The isLast() method has the following prototype:

public boolean isLast() 

This method returns true if the iteration is in its last round. As is the case with isFirst(), subsetting is taken into account. (See the previous section for more information.)

The getBegin() Method

The getBegin() method has the following prototype:

public Integer getBegin() 

This method returns the value of the begin attribute that was specified in the <c:forEach> or <c:forTokens> tag. If no begin tag was specified, null is returned.

The getEnd() Method

The getEnd() method has the following prototype:

public Integer getEnd() 

This method returns the value of the end attribute that was specified in the <c:forEach> or <c:forTokens> tag. If no end tag was specified, null is returned.

The getStep() Method

The getStep() method has the following prototype:

public Integer getStep() 

This method returns the value of the step attribute that was specified in the <c:forEach> or <c:forTokens> tag. If no step tag was specified, null is returned.


    printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    
    Top

    [0672324504/ch05lev1sec2]

     
     


    JSTL. JSP Standard Tag Library Kick Start
    JSTL: JSP Standard Tag Library Kick Start
    ISBN: 0672324504
    EAN: 2147483647
    Year: 2001
    Pages: 93
    Authors: Jeff Heaton

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net