The use of interfaces simplifies taking an existing Java object and making it a tag handler.
There are also two support classes that can be used as base classes:
In many cases, the tag handler only needs to use the tag handler's method
which is invoked when the start tag is encountered. This method needs to access the
attributes of the tag and may also want to access information on the state of the JSP page;
this information is passed to the Tag object before the call to
several setter method calls.
is similar to
, except that it is invoked when the end tag
of the action is encountered. The result of the
invocation indicates whether the
remaining of the page is to be evaluated or not.
A particularly simple and frequent action is one that has an empty body (no text between the
start and the end tag). The Tag Library Descriptor can be used to indicate that the tag is
always intended to be empty; this leads to better error checking at translation time and to
better code quality in the JSP page implementation class.
Actions with Body
Recall that in general, the body of an action may contain other custom and core actions and
scripting elements, as well as uninterpreted template text.
In some cases, an action is only interested in passing through the content of the body. This
can be done using the simple Tag interface by using a special return value in
If an action element can have a non empty body and is interested in the content of that body,
, defined in the BodyTag interface are
The control of the evaluation is actually done based on the result of method invocations as
method is always invoked first and returns an
indicates if the body of the action should be evaluated or not. If so (EVAL_BODY_TAG
return), a nested stream of type
is created and it is passed to the Body
is invoked. Next the body is
evaluated, with the result going into the newly created
object. Finally the
method of the tag handler object is invoked.
If the invocation to
returned SKIP_BODY, the body is not evaluated at all.
methods may use the
object as it sees fit. For example, it
may convert it into a String and use it as an argument. Or it may do some filter action to it
before passing it through to the out stream. Or something else.