In this recipe, you will learn how to use message resource bundles without the f:loadBundle
tag. In other words, we will not load the message resource bundle explicitly as you have seen in the previous two recipes.
We 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.
The first secret resides in faces-config-xml
. Instead of using the message-bundle
tag for registering the message resource bundle, we will use the resource-bundle
tag as shown next:
<application> <resource-bundle> <base-name>custom.Messages</base-name> <var>msg</var> </resource-bundle> </application>
The base-name
tag indicates the location and base name of the message resource bundle, while the var
tag indicates the associated variable's name.
The second secret consists in using the msg
variable. This time we should indicate the desired key in one of these two forms:
<h:outputText value="#{msg['HELLO_WORLD']}"/> <h:outputText value="#{msg.HELLO_WORLD}"/>
This time the f:loadBundle
tasks are moved into faces-config.xml
by using specific entries, therefore we don't need to explicitly use this tag.
A message bundle is not the same as a resource bundle. The message bundle is usually defined when you want to override default JSF conversion or/and validation messages. For the configuration use the resource-bundle
tag instead of message-bundle
. The resource-bundle
tag declares a bundle and a logical name, freeing you from needing to use the f:loadBundle
tag in your JSF view definitions.