Suppose that we have a message resource bundle with the following content (message resource bundles are simply property files with key/value pairs) named myMessages.properties (its
content is not relevant here):
HELLO_WORLD = Hello world message!
In this recipe, you will see how to load and use such a file into a JSF page.
We have developed this recipe with NetBeans 6.8, JSF 2.0, and GlassFish v3. The JSF 2.0 classes were obtained from the NetBeans JSF 2.0 bundled library.
For loading the message resource bundle we use the core tag f:loadBundle
. This loads the bundle and stores it in the request scope. For example:
<f:loadBundle basename="custom.MyMessages" var="msg"/>
Usually, this line appears after an <f:view>
tag and the attribute basename
indicates the location and name of the resource bundle, while the variable's name is specified by the var
attribute of the f:loadBundle
element (for example, msg
).
Now you can display a localized string from our message resource bundle with a JSF component.
<h:outputText value="#{msg.HELLO_WORLD}"/>
The resource bundle is also registered in faces-config.xml
, as shown next:
<application> <message-bundle>custom.MyMessages</message-bundle> </application>