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.

}<br />

text = "???" + key + "???";<br />

}<br />

// Construct and add a FacesMessage containing it<br />

context().addMessage(clientId, new FacesMessage(text));<br />

This method gets the current locale from the UIViewRoot instance of the current request and<br />

loads the localized data for the messages using the getBundle method, passing in the path to the<br />

ResourceBundle and the current locale.<br />

<strong>The</strong> other backing beans call this method by using the key to the message that they are trying to<br />

retrieve from the resource bundle. Here is a call to the message method from<br />

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

message(null, "Quantities Updated");<br />

Localizing Messages<br />

<strong>The</strong> <strong>Java</strong>Server Faces API provides two ways to create messages from a resource bundle:<br />

■ You can register the ResourceBundle instance with the application configuration resource<br />

file and use a message factory pattern to examine the ResourceBundle and to generate<br />

localized FacesMessage instances, which represent single localized messages. <strong>The</strong> message<br />

factory pattern is required to access messages that are registered with the Application<br />

instance. Instead of writing your own message factory pattern, you can use the one included<br />

with the Duke’s Bookstore application. It is called MessageFactory and is located in<br />

tut-install/javaeetutorial5/examples/web/<br />

bookstore6/src/java/com/sun/bookstore6/util/.<br />

■ You can use the FacesMessage class to get the localized string directly from the<br />

ResourceBundle instance.<br />

“Registering Custom Error Messages” on page 445 includes an example of registering a<br />

ResourceBundle in the application configuration resource file.<br />

Creating a Message with a Message Factory<br />

Performing Localization<br />

To use a message factory to create a message, follow these steps:<br />

1. Register the ResourceBundle instance with the application. This is explained in “Registering<br />

Custom Error Messages” on page 445.<br />

2. Create a message factory implementation. You can simply copy the MessageFactory class<br />

included with the Duke’s Bookstore application to your application.<br />

3. Access a message from your application by calling the getMessage(FacesContext,<br />

String, Object) method of the MessageFactory class. <strong>The</strong> MessageFactory class uses the<br />

FacesContext to access the Application instance on which the messages are registered.<br />

<strong>The</strong> String argument is the key that corresponds to the message in the ResourceBundle.<br />

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

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

Saved successfully!

Ooh no, something went wrong!