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 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 file in the bundle folder.

For example, "" in our application look like this :

user_details_form=User Details Form
user_details=User Details
enter_name=Please enter your Name
enter_name_validlength=Please enter more than 3 characters for name
enter_age=Please enter your age
enter_correctage=Please enter correct age
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

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:


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 Please enter name you should represent that key in the JSP page using the format #{msg['']}

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="#{}"></h:outputText>

Your UserDetailsForm.jsp looks like this:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<%@ taglib prefix="f" uri=""%>
<%@ taglib prefix="h" uri=""%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<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="#{}"></h:outputText>
      <h:inputText id="name" value="#{}" required="true"
         <f:validateLength minimum="3" />
      <h:outputText value="#{msg.age}"></h:outputText>
      <h:inputText id="age" value="#{userDetails.age}" required="true"
          <f:validateLength maximum="3" />
       <h:outputText value="#{}"></h:outputText>
       <h:inputText id="email" value="#{}" required="true"
       <h:outputText value="#{msg.dob}"></h:outputText>
       <h:inputText id="dob" value="#{userDetails.dob}" required="true"
           <f:convertDateTime type="date" pattern="MM/dd/yyyy" />
<h:commandButton value="#{msg.submit}"


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.