Configuring Resource Bundle

<< Previous        Next >>

ResourceBundle in JSF




ResourceBundle is mainly used for internationalization of messages. In this section we will discuss how to configure and use the resource bundle in our application. We will discuss about internationalization in the next section. Messages we need to display in the JSP pages can be stored in a properties file, usually referred as resource bundle, instead of hard-coding it in the JSP pages. Keeping the messages separate from the JSP page allows us to easily modify the messages without editing the JSP page. A properties file is just a file with key / value pairs. Properties files are typically saved in the JavaSource/src folder of the application so that during project compilation, this properties file will be copied to the classes folder where the runtime can find it. Properties files should have the file extension properties (eg: messages.properties). Messages in the resource bundle can referred in multiple pages in the application.

To use resource bundle for our application, first we need to create a properties file.

Create a folder named bundle in the JavaSource/src folder of our application. Create messages.properties file in the bundle folder.

For example, "message.properties" in our application look like this :

user_details_form=User Details Form
user_details=User Details
name=Name
enter_name=Please enter your Name
enter_name_validlength=Please enter more than 3 characters for name
age=Age
enter_age=Please enter your age
enter_correctage=Please enter correct age
email=Email
enter_email=Please enter your email
dob=Date Of Birth
enter_dob=Please enter your Date Of Birth
enter_dobpattern=Please enter the date in MM/DD/YYYY
submit=Submit

Next step is to configure resource bundle in the application configuration file. JSF 1.2 provide an efficient way for registering resource bundles with the entire application by using a resource-bundle element in the application's configuration file

The ResourceBundle configuration in the faces-config.xml looks like this:

<application>
    <resource-bundle>
        <base-name>bundle.messages</base-name>
        <var>msg</var>
     </resource-bundle>
</application>


The <base-name> indicates the fully-qualified path of the properties file. The <var> defines the name by which page authors refer to the resource bundle with the expression language.Here ,the resource bundle was registered with a variable called msg. We can reference the messages in resource bundle in the JSP page using the format #{msg.key} . In the resource bundle if the key are composed of multilple word separated by ‘dot’(.),you should represent that key in the JSP page using the format #{msg['key']}.This is a must because ‘dot’(.) is considered as a special character in JSF expression language.

For example,if the properties file contain a key/value pair enter.name= Please enter name you should represent that key in the JSP page using the format #{msg['enter.name']}

Now we can replace the hard coded messages in our JSP pages using the message from resource bundle.

For example,
<h:outputText value="Name” >h:outputText>

we be replaced by
<h:outputText value="#{msg.name}"></h:outputText>

Your UserDetailsForm.jsp looks like this:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<f:view >
<b> <h:outputText value="#{msg.user_details_form}">
</h:outputText> </b>
<p><h:messages style="color: blue" /></p>
<h:form id="userDetailsForm">
   <h:panelGrid columns="2">
      <h:outputText value="#{msg.name}"></h:outputText>
      <h:inputText id="name" value="#{userDetails.name}" required="true"
          requiredMessage="#{msg.enter_name}"
          validatorMessage="#{msg.enter_name_validlength}">
         <f:validateLength minimum="3" />
      </h:inputText>
      <h:outputText value="#{msg.age}"></h:outputText>
      <h:inputText id="age" value="#{userDetails.age}" required="true"
           requiredMessage="#{msg.enter_age}"
           validatorMessage="#{msg.enter_correctage}">
          <f:validateLength maximum="3" />
      </h:inputText>
       <h:outputText value="#{msg.email}"></h:outputText>
       <h:inputText id="email" value="#{userDetails.email}" required="true"
            requiredMessage="#{msg.enter_email}"></h:inputText>
       <h:outputText value="#{msg.dob}"></h:outputText>
       <h:inputText id="dob" value="#{userDetails.dob}" required="true"
            requiredMessage="#{msg.enter_dob}"
            converterMessage="#{msg.enter_dobpattern}">
           <f:convertDateTime type="date" pattern="MM/dd/yyyy" />
        </h:inputText>
</h:panelGrid>
<h:commandButton value="#{msg.submit}"
action="#{userDetails.submitUserDetails}"></h:commandButton>

</h:form>
</f:view>
</body>
</html>



Any number of resource bundle can be loaded in the entire application using resource-bundle element.This eliminates the use of <f:loadbundle> tag in multiple pages, for loading resource bundle. Performance can also be improved using this approach because loading a resource bundle is an expensive operation.

Run your application

To run your web application, right click on BasicJSFProject, click- >run as -> run on server.
Select the server type ,Apache Tomacat v6.0 Server.Click Fininsh.

Open a web browser and type the url : http://localhost:8080/BasicJSFProject/

You will get the same page as shown in our first lesson.


<< Previous        Next  >>

See More Topics:

How page navigation works in JSF?
How to implement Internationalization and Localization in JSF?
How event handling works in JSF?
JSF Request Processing Life Cycle with example.