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.

}<br />

}<br />

}<br />

buffer.append(input[i]);<br />

}<br />

String convertedValue = buffer.toString();<br />

return convertedValue;<br />

During the render response phase, in which the components’ encode methods are called, the<br />

<strong>Java</strong>Server Faces implementation calls the getAsString method in order to generate the<br />

appropriate output. When the <strong>Java</strong>Server Faces implementation calls this method, it passes in<br />

the current FacesContext, the UIComponent whose value needs to be converted, and the bean<br />

value to be converted. Because this converter does a String-to-String conversion, this method<br />

can cast the bean value to a String.<br />

If the value cannot be converted to a String, the method throws an exception, passing the error<br />

message from the ResourceBundle, which is registered with the application. “Registering<br />

Custom Error Messages” on page 445 explains how to register the error messages with the<br />

application. “Performing Localization” on page 388 explains more about working with localized<br />

messages.<br />

If the value can be converted to a String, the method reads the String to a character array and<br />

loops through the array, adding a space after every four characters.<br />

Implementing an Event Listener<br />

As explained in “Event and Listener Model” on page 303, <strong>Java</strong>Server Faces technology supports<br />

action events and value-change events.<br />

Action events occur when the user activates a component that implements ActionSource.<br />

<strong>The</strong>se events are represented by the class javax.faces.event.ActionEvent.<br />

Value-change events occur when the user changes the value of a component that implements<br />

EditableValueHolder. <strong>The</strong>se events are represented by the class<br />

javax.faces.event.ValueChangeEvent.<br />

One way to handle these events is to implement the appropriate listener classes. Listener classes<br />

that handle the action events in an application must implement the interface<br />

javax.faces.event.ActionListener. Similarly, listeners that handle the value-change events<br />

must implement the interface javax.faces.event.ValueChangeListener.<br />

This section explains how to implement the two listener classes.<br />

Implementing an Event Listener<br />

If you need to handle events generated by custom components, you must implement an event<br />

handler and manually queue the event on the component as well as implement an event<br />

listener. See “Handling Events for Custom Components” on page 427 for more information.<br />

Chapter 12 • Developing with <strong>Java</strong>Server FacesTechnology 393

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

Saved successfully!

Ooh no, something went wrong!