The BodyTag interface extends IterationTag by defining additional methods
that let a tag handler manipulate the content of evaluating its body.
It is the responsibility of the tag handler to manipulate the body content.
For example the tag handler may take the body content, convert it into a
String using the bodyContent.getString method and then use it. Or the tag
handler may take the body content and write it out into its enclosing
JspWriter using the bodyContent.writeOut method.
A tag handler that implements BodyTag is treated as one that implements
IterationTag, except that the doStartTag method can return SKIP_BODY,
EVAL_BODY_INCLUDE or EVAL_BODY_BUFFERED.
If EVAL_BODY_INCLUDE is returned, then evaluation happens as in IterationTag.
If EVAL_BODY_BUFFERED is returned, then a BodyContent object will be created
(by code generated by the JSP compiler) to capture the body evaluation. The
code generated by the JSP compiler obtains the BodyContent object by calling
the pushBody method of the current pageContext, which additionally has the
effect of saving the previous out value. The page compiler returns this
object by calling the popBody method of the PageContext class; the call also
restores the value of out.
The interface provides one new property with a setter method and one new
action method.
Properties
There is a new property: bodyContent, to contain the BodyContent object,
where the JSP Page implementation object will place the evaluation (and
reevaluation, if appropriate) of the body. The setter method (setBodyContent)
will only be invoked if doStartTag() returns EVAL_BODY_BUFFERED and the
corresponding action element does not have an empty body.
Methods
In addition to the setter method for the bodyContent property, there is a new
action method: doInitBody(), which is invoked right after setBodyContent()
and before the body evaluation. This method is only invoked if doStartTag()
returns EVAL_BODY_BUFFERED.
Lifecycle
Lifecycle details are described by the transition diagram below. Exceptions
that are thrown during the computation of doStartTag(), setBodyContent(),
doInitBody(), BODY, doAfterBody() interrupt the execution sequence and are
propagated up the stack, unless the tag handler implements the
TryCatchFinally interface; see that interface for details.
Empty and Non-Empty Action
If the TagLibraryDescriptor file indicates that the action must always have
an empty element body, by an <body-content> entry of "empty", then the
doStartTag() method must return SKIP_BODY. Otherwise, the doStartTag() method
may return SKIP_BODY, EVAL_BODY_INCLUDE, or EVAL_BODY_BUFFERED.
Note that which methods are invoked after the doStartTag() depends on both
the return value and on if the custom action element is empty or not in the
JSP page, not how it's declared in the TLD.
If SKIP_BODY is returned the body is not evaluated, and doEndTag() is
invoked.
If EVAL_BODY_INCLUDE is returned, and the custom action element is not empty,
setBodyContent() is not invoked, doInitBody() is not invoked, the body is
evaluated and "passed through" to the current out, doAfterBody() is invoked
and then, after zero or more iterations, doEndTag() is invoked. If the custom
action element is empty, only doStart() and doEndTag() are invoked.
If EVAL_BODY_BUFFERED is returned, and the custom action element is not
empty, setBodyContent() is invoked, doInitBody() is invoked, the body is
evaluated, doAfterBody() is invoked, and then, after zero or more iterations,
doEndTag() is invoked. If the custom action element is empty, only doStart()
and doEndTag() are invoked.