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.

localize dynamic data. If you are not familiar with the basics of localizing web applications, see<br />

Chapter 15, “Internationalizing and Localizing Web Applications.”<br />

All data in the Duke’s Bookstore application have been localized for Spanish, French, German,<br />

and American English. <strong>The</strong> image map on the first page allows you to select your preferred<br />

locale. See Chapter 13, “Creating Custom UI Components,” for information on how the image<br />

map custom component was created.<br />

All the localized data is stored in resource bundles, which are represented as either<br />

ResourceBundle classes or text files, usually with the extension .properties. For more<br />

information about resource bundles, see http://java.sun.com/docs/books/tutorial/i18n/<br />

index.html.<br />

After the application developer has produced a resource bundle, the application architect puts it<br />

in the same directory as the application classes. <strong>The</strong> static text data for the Duke’s Bookstore<br />

application is stored in a ResourceBundle class called BookstoreMessages. <strong>The</strong> error messages<br />

are stored in another resource bundle called ApplicationMessages. After the resource bundles<br />

have been created and before their data can be accessed, they must be made available to the<br />

application, as explained in the following section.<br />

Loading a Resource Bundle<br />

To reference error messages or static data from the page, you first need to make available the<br />

resource bundle containing the data.<br />

To make available resource bundles that contain static data, you need to do one of two things:<br />

■ Register the resource bundle with the application in the configuration file using the<br />

resource-bundle element, as explained in “Registering Custom Localized Static Text” on<br />

page 446.<br />

■ Load the resource bundle into the current view using the loadBundle tag.<br />

Here is an example loadBundle tag from bookstore.jsp:<br />

<br />

Using Localized Data<br />

<strong>The</strong> basename attribute value specifies the fully-qualified class name of the ResourceBundle<br />

class, which in this case is located in the messages package of the bookstore application.<br />

<strong>The</strong> var attribute is an alias to the ResourceBundle class. This alias can be used by other tags in<br />

the page in order to access the localized messages.<br />

In the case of resource bundles that contain error messages, you need to register the resource<br />

bundle with the application in the configuration file using the message-bundle element, as<br />

explained in “Registering Custom Error Messages” on page 445. One exception is if you are<br />

Chapter 11 • Using <strong>Java</strong>Server FacesTechnology in JSP Pages 349

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

Saved successfully!

Ooh no, something went wrong!