background image

Creating a Custom Tag

<< JavaServer Faces Application | Writing the Tag Library Descriptor >>
<< JavaServer Faces Application | Writing the Tag Library Descriptor >>

Creating a Custom Tag

}
public void setTransient(boolean transientValue) {
this.transientValue = transientValue;
}
"Saving and Restoring State" on page 426
describes how a custom component must implement
the saveState(FacesContext) and restoreState(FacesContext, Object) methods.
Creating a Custom Tag
If you implemented a Validator interface rather than implementing a backing bean method
that performs the validation, you need to do one of the following:
Allow the page author to specify the Validator implementation to use with the validator
tag. In this case, the Validator implementation must define its own properties.
"Using a
Custom Validator" on page 375
explains how to use the validator tag.
Create a custom tag that provides attributes for configuring the properties of the validator
from the page. Because the Validator implementation from the preceding section does not
define its attributes, the application developer must create a custom tag so that the page
author can define the format patterns in the tag.
To create a custom tag, you need to do two things:
Write a tag handler to create and register the Validator implementation on the component.
Write a TLD to define the tag and its attributes.
"Using a Custom Validator" on page 375
explains how to use the custom validator tag on the
page.
Writing the Tag Handler
The tag handler associated with a custom validator tag must extend the ValidatorELTag class.
This class is the base class for all custom tag handlers that create Validator instances and
register them on UI components. The FormatValidatorTag class registers the
FormatValidator
instance onto the component.
The FormatValidatorTag tag handler class does the following:
Sets the ID of the validator.
Provides a set of accessor methods for each attribute defined on the tag.
Implements the createValidator method of the ValidatorELTag class. This method
creates an instance of the validator and sets the range of values accepted by the validator.
The formatPatterns attribute of the formatValidator tag supports literals and value
expressions. Therefore, the accessor method for this attribute in the FormatValidatorTag class
must accept and return an instance of ValueExpression:
Creating a Custom Validator
The Java EE 5 Tutorial · September 2007
402