10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Tag Handlers forTags with Attributes<br />

This section describes how to define attributes for a tag handler and how to validate attribute<br />

values.<br />

Defining Attributes in aTag Handler<br />

For each tag attribute, you must define a set method in the tag handler that conforms to the<br />

<strong>Java</strong>Beans architecture conventions. For example, consider the tag handler for the JSTL c:if<br />

tag:<br />

<br />

This tag handler contains the following method:<br />

public void setTest(boolean test) {<br />

this.test = test;<br />

}<br />

As shown by the preceding example, the name of the attribute must match the name of the set<br />

method.<br />

AttributeValidation<br />

<strong>The</strong> documentation for a tag library should describe valid values for tag attributes. When a JSP<br />

page is translated, a web container will enforce any constraints contained in the TLD element<br />

for each attribute.<br />

<strong>The</strong> attributes passed to a tag can also be validated at translation time using the validate<br />

method of a class derived from TagExtraInfo. This class is also used to provide information<br />

about variables defined by the tag (see “TagExtraInfo Class” on page 260).<br />

<strong>The</strong> validate method is passed the attribute information in a TagData object, which contains<br />

attribute-value tuples for each of the tag’s attributes. Because the validation occurs at translation<br />

time, the value of an attribute that is computed at request time will be set to<br />

TagData.REQUEST_TIME_VALUE.<br />

<strong>The</strong> tag has the following TLD attribute element:<br />

<br />

attr1<br />

true<br />

true<br />

<br />

This declaration indicates that the value of attr1 can be determined at runtime.<br />

Programming SimpleTag Handlers<br />

Chapter 8 • CustomTags in JSP Pages 255

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!