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 />

“Registering an Action Listener on a Component” on page 358 explains how to register this<br />

listener onto a component.<br />

Creating a CustomValidator<br />

396<br />

If the standard validators don’t perform the validation checking you need, you can easily create<br />

a custom validator to validate user input. As explained in “Validation Model” on page 305, there<br />

are two ways to implement validation code:<br />

■ Implement a backing bean method that performs the validation.<br />

■ Provide an implementation of the Validator interface to perform the validation.<br />

“Writing a Method to Perform Validation” on page 404 explains how to implement a backing<br />

bean method to perform validation. <strong>The</strong> rest of this section explains how to implement the<br />

Validator interface.<br />

If you choose to implement the Validator interface and you want to allow the page author to<br />

configure the validator’s attributes from the page, you also must create a custom tag for<br />

registering the validator on a component.<br />

If you prefer to configure the attributes in the Validator implementation, you can forgo<br />

creating a custom tag and instead let the page author register the validator on a component<br />

using the validator tag, as described in “Using a Custom Validator” on page 373.<br />

You can also create a backing bean property that accepts and returns the Validator<br />

implementation you create as described in “Writing Properties Bound to Converters, Listeners,<br />

or Validators” on page 387. <strong>The</strong> page author can use the validator tag’s binding attribute to<br />

bind the Validator implementation to the backing bean property.<br />

Usually, you will want to display an error message when data fails validation. You need to store<br />

these error messages in resource bundle, as described in “Creating a Resource Bundle” on<br />

page 388.<br />

After creating the resource bundle, you have two ways to make the messages available to the<br />

application. You can queue the error messages onto the FacesContext programmatically. Or,<br />

you can have the application architect register the error messages using the application<br />

configuration resource file, as explained in “Registering Custom Error Messages” on page 445.<br />

<strong>The</strong> Duke’s Bookstore application uses a general-purpose custom validator (called<br />

tut-install/javaeetutorial5/examples/web/bookstore6/src/java/com/sun/bookstore6/validators/Fo<br />

that validates input data against a format pattern that is specified in the custom validator tag.<br />

This validator is used with the Credit Card Number field on the bookcashier.jsp page. Here is<br />

the custom validator tag:<br />

<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!