background image

Retrieving Localized Messages

<< Setting the Resource Bundle | Dates and Numbers Formatting >>
<< Setting the Resource Bundle | Dates and Numbers Formatting >>

Retrieving Localized Messages

For more information on using this element, see
"Registering Custom Error Messages" on
page 448
.
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" on
page 449
Retrieving Localized Messages
A web component written in the Java programming language retrieves the resource bundle
from the session:
ResourceBundle messages = (ResourceBundle)session.getAttribute(
"messages");
Then it looks up the string associated with the key Talk as follows:
messages.getString(
"Talk");
The JSP versions of the Duke's Bookstore application uses the fmt:message tag to provide
localized strings for messages, HTML link text, button labels, and error messages:
<fmt:message key=
"Talk"/>
For information on the JSTL messaging tags, see
"Messaging Tags" on page 216
.
The JavaServer Faces version of Duke's Bookstore retrieves messages using either the message
or messages tag, or by referencing the message from a tag attribute using a value expression.
You can only use a message or messages tag to display messages that are queued onto a
component as a result of a converter or validator being registered on the component. The
following example shows a message tag that displays the error message queued on the userNo
input component if the validator registered on the component fails to validate the value the user
enters into the component.
<h:inputText id=
"userNo" value="#{UserNumberBean.userNumber}">
<f:validateLongRange minimum=
"0" maximum="10" />
...
<h:message
Providing Localized Messages and Labels
The Java EE 5 Tutorial · September 2007
470