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.

Creating a CustomValidator<br />

400<br />

}<br />

public void setTransient(boolean transientValue) {<br />

this.transientValue = transientValue;<br />

}<br />

“Saving and Restoring State” on page 424 describes how a custom component must implement<br />

the saveState(FacesContext) and restoreState(FacesContext, Object) methods.<br />

Creating a CustomTag<br />

If you implemented a Validator interface rather than implementing a backing bean method<br />

that performs the validation, you need to do one of the following:<br />

■ Allow the page author to specify the Validator implementation to use with the validator<br />

tag. In this case, the Validator implementation must define its own properties. “Using a<br />

Custom Validator” on page 373 explains how to use the validator tag.<br />

■ Create a custom tag that provides attributes for configuring the properties of the validator<br />

from the page. Because the Validator implementation from the preceding section does not<br />

define its attributes, the application developer must create a custom tag so that the page<br />

author can define the format patterns in the tag.<br />

To create a custom tag, you need to do two things:<br />

■ Write a tag handler to create and register the Validator implementation on the component.<br />

■ Write a TLD to define the tag and its attributes.<br />

“Using a Custom Validator” on page 373 explains how to use the custom validator tag on the<br />

page.<br />

Writing theTag Handler<br />

<strong>The</strong> tag handler associated with a custom validator tag must extend the ValidatorELTag class.<br />

This class is the base class for all custom tag handlers that create Validator instances and<br />

register them on UI components. <strong>The</strong> FormatValidatorTag class registers the<br />

FormatValidator instance onto the component.<br />

<strong>The</strong> FormatValidatorTag tag handler class does the following:<br />

■ Sets the ID of the validator.<br />

■ Provides a set of accessor methods for each attribute defined on the tag.<br />

■ Implements the createValidator method of the ValidatorELTag class. This method<br />

creates an instance of the validator and sets the range of values accepted by the validator.<br />

<strong>The</strong> formatPatterns attribute of the formatValidator tag supports literals and value<br />

expressions. <strong>The</strong>refore, the accessor method for this attribute in the FormatValidatorTag class<br />

must accept and return an instance of ValueExpression:<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

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

Saved successfully!

Ooh no, something went wrong!