JSTL provides two actions that you can use to iterate over various types of data:
An overview of the actions listed above is provided in the following pages. A more in-depth examination of those actions can be found in "Iteration Actions" on page 150. JSTL also provides an interface and two classes that let you develop custom iteration actions and access an iteration's status:
An overview of the interface and classes listed above can be found at "Exposed Classes and Interfaces" on page 483. You can also find an in-depth examination of accessing loop status at "Iteration Status" on page 171 and implementing custom iteration actions in "Custom Iteration Actions" on page 178. JSTL Iteration Actions
Syntax: [7]
Syntax #1: Iterates over a collection of objects <c:forEach items [begin] [end] [step] [var] [varStatus]> body content </c:forEach> Syntax #2: Iterates over a set of integer values <c:forEach begin end [step] [var] [varStatus]> body content </c:forEach> Description: You can use the <c:forEach> action to iterate over a data structure, such as an array, map, or collection if you specify that data structure with the items attribute. You can also use <c:forEach> to iterate over integer values if you don't specify the items attribute. Attributes:
Constraints and Error Handling:
In a Nutshell: The <c:forEach> action can iterate over integer values or a data structure that can be one of the following: map, collection, array, or a comma-separated string. The <c:forEach> action can also use an iterator or an enumeration to iterate over an underlying collection.
Syntax: [8]
<c:forTokens items delims [begin] [end] [step] [var] [varStatus]> body content </c:forTokens> Description: The <c:forTokens> action iterates over a string of tokens delimited by delimiters that you specify with the delims attribute. Attributes:
Constraints and Error Handling:
In a Nutshell: The <c:forEach> action can iterate over tokens in a string as long as those tokens are delimited by commas. If you need to iterate over a string whose tokens are delimited by characters other than commas, you can use the <c:forTokens> action. The <c:forTokens> action is especially handy when you need to iterate over strings with multiple tokens that represent nested data; see "The <c:forTokens> Action" on page 166 for an example of that usage. Exposed Classes and InterfacesThe JSTL iteration actions expose one interface and two classes:
The classes and interface listed above are discussed below.
Definition: interface LoopTag { public Object getCurrent() public LoopTagStatus getLoopStatus() } Description: The <c:forEach> and <c:forTokens> actions have tag handlers that implement the LoopTag interface. You can take advantage of that implementation to implement custom actions that collaborate with <c:forEach> and <c:forTokens> actions; see "Collaboration Custom Actions" on page 178 for more information about implementing collaboration custom actions.
Definition: class LoopTagSupport { public LoopTagSupport() protected abstract Object next() throws JspTagException protected abstract boolean hasNext() throws JspTagException protected abstract void prepare() throws JspTagException protected void validateBegin() throws JspTagException protected void validateEnd() throws JspTagException protected void validateStep() throws JspTagException public Object getCurrent() throws JspTagException public LoopTagStatus getLoopStatus() public void setVar(String) public void setVarStatus(String) } Description: To implement iteration custom actions, extend the LoopTagSupport class, which provides convenience methods for implementing those types of custom actions. The LoopTagSupport class is also the superclass of the <c:forEach> and <c:forTokens> tag handlers. See "Custom Iteration Actions" on page 178 for more information about developing iteration custom actions.
Definition: class LoopTagStatus { public Object getCurrent() public int getIndex() public int getCount() public boolean isFirst() public boolean isLast() public Integer getBegin() public Integer getEnd() public Integer getStep() } Description: The LoopTagStatus interface provides information about the status of an iteration. When you specify the varStatus attribute for <c:forEach> or <c:forTokens>, an object that implements the LoopTagStatus interface is made available in the body of those actions. You can use that object to obtain information about the current iteration. |