Integrating Custom Iteration Actions

JSTL offers two utilities for customized iterations: a support class that can be extended for application-specific iteration actions and interfaces that actions nested in the body of an iteration action can use to get information about the iteration status.

Implementing a Custom Iteration Action

The JSTL <c:forEach> action is so flexible that it probably covers most cases, but to help develop application-specific iteration actions when needed, JSTL provides a base class for this as well. It’s named javax.servlet.jsp.jstl.core.LoopTagSupport :

public abstract class LoopTagSupport 
  extends javax.servlet.jsp.tagext.TagSupport
  implements javax.servlet.jsp.jstl.core.LoopTag,
    javax.servlet.jsp.tagext.IterationTag,
    javax.servlet.jsp.tagext.TryCatchFinally

The class has the following fields a subclass can access:

protected int begin
protected int end
protected int step
protected String itemId
protected String statusId
protected boolean beginSpecified
protected boolean endSpecified
protected boolean stepSpecified

These variables hold the value of the corresponding attributes. The variable names for the var and varStatus attributes (itemId and statusId) are, unfortunately, not in sync with the attribute names, due to an oversight when the attribute naming conventions where changed. Nobody’s perfect. For the int variables, there are also boolean variables that tell if the corresponding attributes where set or not.

Here are the main methods a subclass must ...

Get JavaServer Pages, Second 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.