The Java EE 5 Tutorial

Setting the Resource Bundle

After the locale is set, the controller of a web application typically retrieves the resource bundle for that locale and saves it as a session attribute (see Associating Objects with a Session) for use by other components:

messages = ResourceBundle.getBundle("com.sun.bookstore.messages.BookstoreMessages", 
    locale);
session.setAttribute("messages", messages);

The resource bundle base name for the JSTL versions of Duke’s Bookstore is set at deployment time through a context parameter. When a session is initiated, the resource bundle for the user’s locale is stored in the localization context. It is also possible to override the resource bundle at runtime for a given scope using the fmt:setBundle tag and for a tag body using the fmt:bundle tag.

The JavaServer Faces version of Duke’s Bookstore uses two methods for setting the resource bundle. One method is letting the JSP pages set the resource bundle using the f:loadBundle tag. This tag loads the correct resource bundle according to the locale stored in FacesContext.

<f:loadBundle basename="messages.BookstoreMessages"
    var="bundle"/>

For information on this tag, see Loading a Resource Bundle.

Another way a JavaServer Faces application sets the resource bundle is by configuring it in the application configuration file. There are two XML elements that you can use to set the resource bundle: message-bundle and resource-bundle.

If the error messages are queued onto a component as a result of a converter or validator being registered on the component, then these messages are automatically displayed on the page using the message or messages tag. These messages must be registered with the application using the message-bundle tag:

<message-bundle>
    resources.ApplicationMessages
</message-bundle>

For more information on using this element, see Registering Custom Error Messages.

Resource bundles containing messages that are explicitly referenced from a JavaServer Faces tag attribute using a value expression must be registered using the resource-bundle element of the configuration file:

<resource-bundle>
    <base-name>com.sun.bookstore6.resources.CustomMessages</base-name>
    <var>customMessages</var>
</resource-bundle>

For more information on using this element, see Registering Custom Localized Static Text