background image

Implementing an Event Listener

<< The getAsObject Method | Implementing Value-Change Listeners >>
<< The getAsObject Method | Implementing Value-Change Listeners >>

Implementing an Event Listener

}
}
buffer.append(input[i]);
}
String convertedValue = buffer.toString();
return convertedValue;
}
During the render response phase, in which the components' encode methods are called, the
JavaServer Faces implementation calls the getAsString method in order to generate the
appropriate output. When the JavaServer Faces implementation calls this method, it passes in
the current FacesContext, the UIComponent whose value needs to be converted, and the bean
value to be converted. Because this converter does a String-to-String conversion, this method
can cast the bean value to a String.
If the value cannot be converted to a String, the method throws an exception, passing the error
message from the ResourceBundle, which is registered with the application.
"Registering
Custom Error Messages" on page 448
explains how to register the error messages with the
application.
"Performing Localization" on page 390
explains more about working with localized
messages.
If the value can be converted to a String, the method reads the String to a character array and
loops through the array, adding a space after every four characters.
Implementing an Event Listener
As explained in
"Event and Listener Model" on page 305
, JavaServer Faces technology supports
action events and value-change events.
Action events occur when the user activates a component that implements ActionSource.
These events are represented by the class javax.faces.event.ActionEvent.
Value-change events occur when the user changes the value of a component that implements
EditableValueHolder
. These events are represented by the class
javax.faces.event.ValueChangeEvent
.
One way to handle these events is to implement the appropriate listener classes. Listener classes
that handle the action events in an application must implement the interface
javax.faces.event.ActionListener
. Similarly, listeners that handle the value-change events
must implement the interface javax.faces.event.ValueChangeListener.
This section explains how to implement the two listener classes.
If you need to handle events generated by custom components, you must implement an event
handler and manually queue the event on the component as well as implement an event
listener. See
"Handling Events for Custom Components" on page 429
for more information.
Implementing an Event Listener
Chapter 12 · Developing with JavaServer Faces Technology
395