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 "&lt;h2&gt;message.test4&lt;/h2&gt;"
message.param1 = This is "message.param1" - {0}
message.param2 = This is "message.param2" - {0} and {1}

Project folder structure.

jsf2-resource-bundles-folder

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 "&lt;h2&gt;message.test4&lt;/h2&gt;"

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

jsf2-resource-bundles-example

Download Source Code

Download it – JSF-2-Resource-Bundles-Example.zip (8KB)
Tags :
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

Related Posts

Popular Posts