JSF 2.0 and Resource Bundles example
In this tutorial, we demonstrate the use of resource bundle to display messages in JSF 2.0. For maintainability concern, it’s always recommended to put all the messages in properties file, instead of hard-code the message in page directly.
1. Properties File
Create a properties file, contains message for the page, and put it into the project’s resource folder, see figure below
messages.properties
message = This is "message" message.test1 = This is "message.test1" message.test2 = This is "<h2>message.test3</h2>" message.test3 = This is "<h2>message.test4</h2>" message.param1 = This is "message.param1" - {0} message.param2 = This is "message.param2" - {0} and {1}
Project folder structure.

2. Using Resource Bundles
There are two ways to load the properties file into JSF 2.0.
1. Global Resource Bundle
To load the properties file globally, so that all the jsf pages can access the messages. You can create a “faces-config.xml” file and declare the properties file explicitly.
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?> <faces-config xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" version="2.0"> <application> <resource-bundle> <base-name>com.mkyong.messages</base-name> <var>msg</var> </resource-bundle> </application> </faces-config>
2. Local Resource Bundle
To load the properties file locally, or for specified page only. Declare the <f:loadBundle /> tag in the page that need to access the message in the messages.properties.
<f:loadBundle basename="com.mkyong.messages" var="msg"/>3. JSF 2.0 Pages
In this case, the messages.properties file is given a name of “msg“, to access the message, just use “msg.key“.
hello.xhtml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"> <h:body> <h2>JSF 2.0 and Resource Bundles Examples</h2> <ol> <li><h:outputText value="#{msg.message}" /></li> <li><h:outputText value="#{msg['message.test1']}" /></li> <li><h:outputText value="#{msg['message.test2']}" /></li> <li><h:outputText value="#{msg['message.test2']}" escape="false" /></li> <li><h:outputText value="#{msg['message.test3']}" /></li> <li><h:outputText value="#{msg['message.test3']}" escape="false" /></li> <li> <h:outputFormat value="#{msg['message.param1']}"> <f:param value="param0" /> </h:outputFormat> </li> <li> <h:outputFormat value="#{msg['message.param2']}"> <f:param value="param0" /> <f:param value="param1" /> </h:outputFormat> </li> </ol> </h:body> </html>
4. How it works?
Example 1
A normal way to access the message.
<h:outputText value="#{msg.message}" />
//properties file
message = This is "message"Example 2
For a key that has a dot “.” as name, you can’t use the normal way #{msg.message.test1}, it will not work. Instead, you should use bracket like #{msg['message.test1']}.
<h:outputText value="#{msg['message.test1']}" />
//properties file
message.test1 = This is "message.test1"Example 3
To display HTML tag in the message, just add the “escape” attribute and set it to false.
<h:outputText value="#{msg['message.test2']}" /> <h:outputText value="#{msg['message.test2']}" escape="false" /> <h:outputText value="#{msg['message.test3']}" /> <h:outputText value="#{msg['message.test3']}" escape="false" /> //properties file message.test2 = This is "<h2>message.test3</h2>" message.test3 = This is "<h2>message.test4</h2>"
Example 4
For a parameter message, just use the <h:outputFormat /> and <f:param / > tag.
<h:outputFormat value="#{msg['message.param1']}"> <f:param value="param0" /> </h:outputFormat> <h:outputFormat value="#{msg['message.param2']}"> <f:param value="param0" /> <f:param value="param1" /> </h:outputFormat> //properties file message.param1 = This is "message.param1" - {0} message.param2 = This is "message.param2" - {0} and {1}
5. Demo
URL : http://localhost:8080/JavaServerFaces/hello.jsf








Great tutorial!! :)
One question :- when I load the resource-bundle locally in a page…should I add the entries in faces-config.xml? Or directly use it in the xhtml page.
I tried using it directly, did not work :/
Thanks heaps. I have to admit that your style of explaining and going through the JSF basics suits me best.
I’d like to say that your article help me a lot.
Almost articles I google don’t mention about resource bundle hierarchical message key.
They always give example such as
//properties file
message_param1 = This is “message.param1″ – {0}
message_param2
Once again, you rock! You help me a lot!
Thank you very much!
Welcome, good to know that it works to you.
Hi,
Nice explanation and nice presentation including workspace structure
regards
Satya
Hey there…
Nice presentation, but I am missing a point, to be honest. The way you show resource bundles was kind of already working with JSF 1.2. Since 2.0, there is also locales and libraries. Maybe you could also state something about this?
Apart from that a good tut for starters! Thank you!
[...] JSF 2.0 and Resource Bundles example In this tutorial, we show you the use of resource bundle to display the messages in the JSF 2.0. For maintainability concern, it’s recommended to put all the messages in properties file, instead of hard-code the message in the page directly. [...]
nice tutorial!
Little bit confusing in “4. How it work?” (Should be works instead of work) but it’s ok.
Keep it up! :)
Thanks for correction
[...] JSF 2 & Resource Bundle This article was posted in JSF2 category. Oracle Magazine – Free Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for Java's developers and DBAs, and more. [...]
[...] JSF 2 resource bundles example This article was posted in JSF2 category. Oracle Magazine – Free Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for Java's developers and DBAs, and more. [...]
If I were you I should make it a little bit more clear that step 1 and 2 are alternatives.
You might also want to work a little bit on your English:
“In this tutorial, it shows the use of resource bundle to display the messages in the JSF 2.0. ”
->
“In this tutorial, we demonstrate the use of a resource bundle to display messages in JSF 2.0.” or
“In this tutorial, we show how to use a resource bundle to display messages in JSF 2.0.”
Basically, you don’t seem to understand how to use articles in English. You use them where they shouldn’t be used and you omit them where they are needed.
Thanks for your kind comment and correction
@Mkyong
One of the less confusing things for me while trying to learn how to load a
DBResourceBundle.classis the style of English used in a Tutorial.
Your Tutorial is fine, Your English is fine, Everything is fine.
Great Website!
Thank you very much