Name

<c:forEach>

Synopsis

The <c:forEach> action evaluates its body a fixed number of times or once for each element in a collection. The current element (or the current index if no collection is specified) and the iteration status can be exposed to action elements in the body through nested variables.

The action accepts collections of the types listed in the Attributes table. The type of the current element is the type of the underlying collection, with two exceptions. For an array of a primitive type, the current element is exposed as an instance of the corresponding wrapper class (Integer, Float, etc.) For a java.util.Map, the current element is exposed as a java.util.Map.Entry.

Syntax 1: Iteration over collection elements

<c:forEach items="collection" [var="var"] [varStatus="varStatus"]
  [begin="startIndex"] [end="stopIndex"] [step="increment"]>
  JSP elements
</c:forEach>

Syntax 2: Fixed number of iterations

<c:forEach [var="var"] [varStatus="varStatus"]
  begin="startIndex" end="stopIndex" [step="increment"]>
  JSP elements
</c:forEach>

Attributes

Attribute name

Java type

Dynamic value accepted

Description

begin
int

Yes

The start index, 0-based when used with a collection. Default is 0 for a collection.

end
int

Yes

The stop index (inclusive), 0-based when used with a collection. The default is the last element for a collection. If end is less than begin, the body is not evaluated at all.

items

java.util.Collection, java.util.Iterator, java.util.Enumeration, java.util.Map

Get JavaServer Pages, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.