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.TryCatchFinallyThe 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 whether the
corresponding attributes were set.
Here are the main methods a subclass must implement:
protected abstract void prepare( ) throws javax.servlet.jsp.JspTagException protected abstract Object next( ) throws javax.servlet.jsp.JspTagException protected abstract boolean hasNext( ) throws javax.servlet.jsp.JspTagException
The prepare( ) method prepares for the iteration, for instance ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access