Message resource bundles without f:loadBundle

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.

Getting ready

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.

How to do it...

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}"/>

How it works...

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.

There's more...

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.

See also

The code bundled with this book contains a complete example of this recipe. The project can be opened with NetBeans 6.8 and it is named: Message_resource_bundle_without_loadBundle.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset