Thursday, April 3, 2008

Tag handler interfaces in Custom Tag

Tag handler interfaces

The tag handlers must implement Tag , BodyTag , or IterationTag interfaces. These interfaces are contained in the javax.servlet.jsp.tagext package.

Tag handler methods defined by these interfaces are called by the JSP engine at various points during the evaluation of the tag.

Tag interface
The Tag interface defines the basic protocol between a tag handler and JSP container. It is the base interface for all tag handlers and declares the main lifecycle methods of the tag.

  • The setPageContext() method is called first by the container. The pageContext implicit object of the JSP page is passed as the argument.
  • The setParent() method is called next, which sets the parent of the tag handler.
  • For each attribute of the custom tag, a setter method is invoked next.
  • The doStartTag() method can perform initializations. It returns the value EVAL_BODY_INCLUDE or SKIP_BODY .
  • The doEndTag() method can contain the cleanup code for the tag. It returns the value EVAL_PAGE or SKIP_PAGE .
  • The release() method is called when the tag handler object is no longer required.

IterationTag interface
The IterationTag interface extends Tag by defining one additional method that controls the reevaluation of its body.

  • IterationTag provides a new method: doAfterBody() .
  • If doStartTag() returns SKIP_BODY , the body is skipped and the container calls doEndTag() .
  • If doStartTag() returns EVAL_BODY_INCLUDE , the body of the tag is evaluated and included, and the container invokes doAfterBody() .
  • The doAfterBody() method is invoked after every body evaluation to control whether the body will be reevaluated.
  • If doAfterBody() returns IterationTag.EVAL_BODY_AGAIN , then the body will be reevaluated. If doAfterBody() returns Tag.SKIP_BODY , then the body will be skipped and doEndTag() will be evaluated instead.

BodyTag interface
The BodyTag interface extends IterationTag by defining additional methods that let a tag handler manipulate the content of evaluating its body:

  • The doStartTag() method can return SKIP_BODY , EVAL_BODY_INCLUDE , or EVAL_BODY_BUFFERED .
  • If EVAL_BODY_INCLUDE or SKIP_BODY is returned, then evaluation happens as in IterationTag .
  • If EVAL_BODY_BUFFERED is returned, setBodyContent() is invoked, doInitBody() is invoked, the body is evaluated, doAfterBody() is invoked, and then, after zero or more iterations, doEndTag() is invoked. The doAfterBody() element returns EVAL_BODY_AGAIN or EVAL_BODY_BUFFERED to continue evaluating the page and SKIP_BODY to stop the iteration.

No comments:

Post a Comment