Developing an Iterating Action

As I alluded to earlier, a tag handler can ask the container to evaluate the action element’s body repeatedly until some condition is true. For each evaluation, the result can be different because variables used in the body may change their values. An example of an iterating action is the JSTL <c:forEach> action. It can iterate over the element’s body once for each element in a collection.

A tag handler that evaluates its body repeatedly implements the IterationTag interface, which contains only one method:

public int doAfterBody( ) throws JspException

Called by the container after it has processed the action element’s body.

If you have worked with JSP 1.1 previously, this method may look familiar. It was part of the BodyTag interface in JSP 1.1 but was moved to the new IterationTag interface in JSP 1.2 to provide for a more efficient handling of iteration actions that don’t need access to their element body.

A tag handler that implements the IterationTag interface is at first handled the same way as a tag handler implementing the Tag interface: the container calls all property setter methods and the doStartTag( ) method. Then things divert slightly, as illustrated in Figure 20-3.

IterationTag interface methods

Figure 20-3. IterationTag interface methods

After the call to doStartTag( ), the doAfterBody( ) method may be called before the doEndTag( ) method is finally called. ...

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.