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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using a CustomValidator<br />

To register a custom validator on a component, you must do one of the following:<br />

■ Nest the validator’s custom tag inside the tag of the component whose value you want to be<br />

validated.<br />

■ Nest the standard validator tag within the tag of the component and reference the custom<br />

Validator implementation from the validator tag.<br />

Here is the custom formatValidator tag from the ccno field on the bookcashier.jsp page of<br />

the Duke’s Bookstore application:<br />

<br />

<br />

<br />

<br />

Using Custom Objects<br />

This tag validates the input of the ccno field against the patterns defined by the page author in<br />

the formatPatterns attribute.<br />

You can use the same custom validator for any similar component by simply nesting the custom<br />

validator tag within the component tag.<br />

“Creating a Custom Validator” on page 396 describes how to create the custom validator and its<br />

custom tag.<br />

If the application developer who created the custom validator prefers to configure the attributes<br />

in the Validator implementation rather than allow the page author to configure the attributes<br />

from the page, the developer will not create a custom tag for use with the validator.<br />

In this case, the page author must nest the validator tag inside the tag of the component whose<br />

data needs to be validated. <strong>The</strong>n the page author needs to do one of the following:<br />

1. Set the validator tag’s validatorId attribute to the ID of the validator that is defined in the<br />

application configuration resource file. “Registering a Custom Validator” on page 447<br />

explains how to configure the validator in the application configuration resource file.<br />

2. Bind the custom Validator implementation to a backing bean property using the<br />

validator tag’s binding attribute, as described in “Binding Converters, Listeners, and<br />

Validators to Backing Bean Properties” on page 367.<br />

<strong>The</strong> following tag registers a hypothetical validator on a component using a validator tag and<br />

references the ID of the validator:<br />

Chapter 11 • Using <strong>Java</strong>Server FacesTechnology in JSP Pages 373

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

Saved successfully!

Ooh no, something went wrong!