Resource Bundles in JSF 2.0 Applications

Setting up resource message bundles in JSF to provide multilingal messages and captions is often overlooked when first creating an application. Leaving it till later in the project means you will have to go back and manually change the constants over to resource based values. Resource bundles JSF 1.2 were far from perfect but fortunately, using resource bundles in JSF 2.0 is very easy and this tutorial will show you how to add bundles and use them in your JSF 2.0 pages.

For this example, we’ll create a new application using the jee6-servlet-basic-archetype Knappsack Maven archetype. The full source can be downloaded and run using mvn clean jetty:run from the command line.

Message resources are stored in properties files which consist of name value pairs that binds a message key string with the message value. We’ll use the following example

1 firstName=First Name
2 lastName=Last Name
3 forgotPassword=Forgot Password?
4 usernameTaken={0} is already taken

This file needs to be saved and referenced in a package, which would normally be in the same place as your source code, but Maven provides a separate area for resources. Save the file in thesrc/main/resources/org/fluttercode/resourcedemo/ with the name Open up thefaces-config file and add the following XML :

1 <application>
2     <resource-bundle>
3         <base-name>org.fluttercode.resourcedemo.MessageResources</base-name>
4         <var>msgs</var>
5     </resource-bundle>
6 </application>

Here, we have told JSF about the resource bundle and assigned a variable name to it. Now we will go and add a reference to one of the messages in our home page. Open home.xhtml and replace the initial message with the following :

1 <h:outputText  value="#{msgs.firstName}"/>

This is a JSF output text component that gets its value from the resource bunde, in this case, thefirstName value. If you are using JBoss Tools, you will see that it can perform autocompletion for you on both the msgs value and the actual property keys on the msgs resources. It also displays the actual property value in the preview window for the page. If you run the app now by typing mvn jetty:run in the command line, you will see that the word First Name appears in the page.

Access Resources From Code

Typically, in your application you will generate messages for the user that also needs to be obtained from the resource bundle. To do this, we will create a bean that can fetch the resource bundle for us and extract strings from it.

01 public class MessageProvider {
03     private ResourceBundle bundle;
05     public ResourceBundle getBundle() {
06         if (bundle == null) {
07             FacesContext context = FacesContext.getCurrentInstance();
08             bundle = context.getApplication().getResourceBundle(context, "msgs");
09         }
10         return bundle;
11     }
13     public String getValue(String key) {
15         String result = null;
16         try {
17             result = getBundle().getString(key);
18         catch (MissingResourceException e) {
19             result = "???" + key + "??? not found";
20         }
21         return result;
22     }
24 }

This class fetches the resource bundle from the faces context which will determine the best bundle to use based on the supported locales and the client locale. The second method uses the first method to fetch a string resource from the bundle.
We’ll create a JSF backing bean to use this bean to return a message to the user.

1 @Named
2 @RequestScoped
3 public class SomeBean {
5     public String getMessage() {
6         String msg = new MessageProvider().getValue("someMessage");
7         return MessageFormat.format(msg, "SomeValue");
8     }
9 }

In our home.jsf page, we display our message by adding

1 Message = #{someBean.message}

Which results in the following phrase being displayed :

1 Message = SomeValue is not valid

Having just the one default properties file in place is the bare minimum for using string resource bundles in your applications, and I would recommend using that for any application, even if it is never going to be multi-lingual. At the very least, it keeps your string constants in one place and at best, it makes it really easy to support multiple languages at a later date.




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s