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 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 ...
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.