background image

Tag Handlers for Basic Tags

<< Programming Simple Tag Handlers | Setting Dynamic Attributes >>
<< Programming Simple Tag Handlers | Setting Dynamic Attributes >>

Tag Handlers for Basic Tags

Tag Handlers for Basic Tags
The handler for a basic tag without a body must implement the doTag method of the SimpleTag
interface. The doTag method is invoked when the end element of the tag is encountered.
The basic tag discussed in the first section, <tt:basic />, would be implemented by the
following tag handler:
public HelloWorldSimpleTag extends SimpleTagSupport {
public void doTag() throws JspException, IOException {
getJspContext().getOut().write(
"Hello, world.");
}
}
Tag Handlers for Tags with Attributes
This section describes how to define attributes for a tag handler and how to validate attribute
values.
Defining Attributes in a Tag Handler
For each tag attribute, you must define a set method in the tag handler that conforms to the
JavaBeans architecture conventions. For example, consider the tag handler for the JSTL c:if
tag:
<c:if test=
"${Clear}">
This tag handler contains the following method:
public void setTest(boolean test) {
this.test = test;
}
As shown by the preceding example, the name of the attribute must match the name of the set
method.
Attribute Validation
The documentation for a tag library should describe valid values for tag attributes. When a JSP
page is translated, a web container will enforce any constraints contained in the TLD element
for each attribute.
The attributes passed to a tag can also be validated at translation time using the validate
method of a class derived from TagExtraInfo. This class is also used to provide information
about variables defined by the tag (see
"TagExtraInfo Class" on page 262
).
Programming Simple Tag Handlers
Chapter 8 · Custom Tags in JSP Pages
257