background image

Event and Listener Model

<< Conversion Model | Data-Model Events >>
<< Conversion Model | Data-Model Events >>

Event and Listener Model

When a component is bound to an object, the application has two views of the component's
data:
The model view, in which data is represented as data types, such as int or long.
The presentation view, in which data is represented in a manner that can be read or
modified by the user. For example, a java.util.Date might be represented as a text string
in the format mm/dd/yy or as a set of three text strings.
The JavaServer Faces implementation automatically converts component data between these
two views when the bean property associated with the component is of one of the types
supported by the component's data. For example, if a UISelectBoolean component is
associated with a bean property of type java.lang.Boolean, the JavaServer Faces
implementation will automatically convert the component's data from String to Boolean. In
addition, some component data must be bound to properties of a particular type. For example, a
UISelectBoolean
component must be bound to a property of type boolean or
java.lang.Boolean
.
Sometimes you might want to convert a component's data to a type other than a standard type,
or you might want to convert the format of the data. To facilitate this, JavaServer Faces
technology allows you to register a Converter implementation on UIOutput components and
components whose classes subclass UIOutput. If you register the Converter implementation on
a component, the Converter implementation converts the component's data between the two
views.
You can either use the standard converters supplied with the JavaServer Faces implementation
or create your own custom converter.
To create and use a custom converter in your application, three things must happen:
The application developer must implement the Converter class. See
"Creating a Custom
Converter" on page 393
.
The application architect must register the Converter with the application. See
"Registering
a Custom Converter" on page 451
.
The page author must refer to the Converter object from the tag of the component whose
data must be converted. See
"Using a Custom Converter" on page 374
.
Event and Listener Model
The JavaServer Faces event and listener model is similar to the JavaBeans event model in that it
has strongly typed event classes and listener interfaces that an application can use to handle
events generated by UI components.
An Event object identifies the component that generated the event and stores information
about the event. To be notified of an event, an application must provide an implementation of
the Listener class and must register it on the component that generates the event. When the
User Interface Component Model
Chapter 10 · JavaServer Faces Technology
305