The IterationTag Interface in JSP

By: Henry Viewed: 153234 times  Printer Friendly Format    


The IterationTag interface was introduced as a part of the JSP 1.2 specification in an attempt to simplify the procedure for evaluating and reevaluating body content multiple times. This was previously possible with the JSP 1.1 BodyTag interface, although the implementation details were complex.

 IterationTag.java

package javax.servlet.jsp.tagext;
import javax.servlet.jsp.JspException;
public interface IterationTag extends Tag {
public final static int EVAL_BODY_AGAIN = 2;
int doAfterBody() throws JspException;
}

The interface itself is fairly small, and this is because it extends the Tag interface. Effectively, a single method has been added to the tag life cycle called doAfterBody(), and it’s this method that provides you with the ability to ask that a tag’s body content is evaluated more than once. Let’s see how this affects the tag life cycle.

The Iteration Tag Life Cycle
Figure below illustrates the iteration tag life cycle and shows where the new doAfterBody() method fits in with the life cycle of a custom tag.

To support iteration, the doAfterBody() method is called after doStartTag(). The return value of doAfterBody() indicates whether the tag body is evaluated again or processing continues to the doEndTag() method.

Setting the Context

The first thing that happens is that the contextual information (the PageContext and parent Tag) is passed to the tag handler instance.

The Start Tag

After the context has been set, the doStartTag() method is called and one of two things can happen. First, the doStartTag() method can return SKIP_BODY, meaning that the body content is ignored and processing proceeds to the doEndTag() method as before. Alternatively, a value of EVAL_BODY_INCLUDE can be returned from the doStartTag() method, signaling to the JSP container that the body content should be evaluated and included in the page. Again, this is the same as with the Tag interface, although it’s after the body content has been evaluated that things start to change.

After the Body

After the body content has been evaluated, the new doAfterBody() method is called, the primary purpose of which is to determine whether the body content should be reevaluated. If this is the case, the method should return the EVAL_BODY_AGAIN constant defined within the IterationTag interface. On the other hand, and when no more evaluations of the body content should happen, the SKIP_BODY value should be returned.

The End Tag

Finally, regardless of whether or not the body was evaluated and reevaluated multiple times, the doEndTag() method is called in the same way as the other tags that you’ve seen in this chapter. Again, possible return values are EVAL_PAGE and SKIP_PAGE.



Most Viewed Articles (in JSP )

Latest Articles (in JSP)

Comment on this tutorial