Developing an Iterating Action

For a simple tag handler, iterative evaluation of the body is simply done within the doTag( ) method, as described earlier. A classic tag handler doesn’t have this luxury. Instead, it must ask the container to evaluate the action element’s body repeatedly until some condition is true. To do so, it implements the IterationTag interface, which contains only one method: public int doAfterBody( ) throws JspException , which is called by the container after it has processed the action element’s 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 21-5.

IterationTag interface methods
Figure 21-5. IterationTag interface methods

After the call to doStartTag( ), the doAfterBody( ) method may be called any number of times before the doEndTag( ) method is called.

Let’s implement the same scaled-down version of the <c:forEach> action that I used to illustrate iteration with a simple tag handler again but this time as a classic tag handler. As before, the scaled down version supports only Collection data structures. The class tag handler class is shown in Example 21-9.

Example 21-9. A tag handler implementing the IterationTag interface
package com.ora.jsp.tags.xmp; import java.util.*; ...

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.