background image

Using a Custom Converter

<< Using Custom Objects | Using a Custom Validator >>
<< Using Custom Objects | Using a Custom Validator >>

Using a Custom Converter

The next three sections describe how to use the custom converter, validator, and UI
components included in the Duke's Bookstore application.
Using a Custom Converter
As described in the previous section, to apply the data conversion performed by a custom
converter to a particular component's value, you must either reference the custom converter
from the component tag's converter attribute or from a converter tag nested inside the
component tag.
If you are using the component tag's converter attribute, this attribute must reference the
Converter
implementation's identifier or the fully-qualified class name of the converter. The
application architect provides this identifier when registering the Converter implementation
with the application, as explained in
"Registering a Custom Converter" on page 451
.
"Creating a
Custom Converter" on page 393
explains how a custom converter is implemented.
The identifier for the credit card converter is CreditCardConverter. The
CreditCardConverter
instance is registered on the ccno component, as shown in this tag from
the bookcashier.jsp page:
<h:inputText id=
"ccno"
size=
"19"
converter=
"CreditCardConverter"
required=
"true">
...
</h:inputText>
By setting the converter attribute of a component's tag to the converter's identifier or its class
name, you cause that component's local value to be automatically converted according to the
rules specified in the Converter implementation.
Instead of referencing the converter from the component tag's converter attribute, you can
reference the converter from a converter tag nested inside the component's tag. To reference
the custom converter using the converter tag, you do one of the following:
Set the converter tag's converterId attribute to the Converter implementation's identifier
defined in the application configuration file.
Bind the Converter implementation to a backing bean property using the converter tag's
binding
attribute, as described in
"Binding Converters, Listeners, and Validators to Backing
Bean Properties" on page 369
.
Using Custom Objects
The Java EE 5 Tutorial · September 2007
374