This article was last updated on : November 4th, 2016 at 10:36 am

Tutorial : properties file in JSF with example

Message or Properties file in JSF

Objective of Example : Using .properties File in JSF :
1. In this example we will demonstrate how to use .properties or message file in jsf.
2. The idea behind using properties or message files in JSF is write one place use anywhere in application.
3. Eclipse Hierarchy of this project is given below.

Image to show eclipse architecture how to load properties file in JSF
“properties file in JSF Dynamic Web Project

Please follow the below steps to do this using properties or resource bundle file in JSF.

1. Create A JSF Dynamic Web Project in Eclipse :
Please create a JSF dynamic web project with name “UsingPropertiesFileInJSF”.
Please see this tutorial “How to create Dynamic web Application in JSF”.

2. Create JSP pages for this project :
Now we have to create two JSP pages.
1. usingResourceBundle.jsp – To use a properties file in JSP page we use

2.  messageBundle.properties – This is the properties files where we will store the messages. It will be in key-value format. Inside the JSF page we will use the key to find the value.
Please see in JSP page basename=”com.sitenol.messageBundle.messageBundle” so after compilation After compilation of this web project messageBundle.properties file should be in folder –
WEB-INF/classes/com/sitenol/resourceBundle/messageBundle
1. messageBundle.properties


label_greeting = Welcome to Sitenol.com.
label_current_year=Current Year is 2013.
label_java_j2ee_technologies=Core Java, Servlet, JSP, JSF.

2. usingResourceBundle.jsp


<%@page import="java.util.Calendar"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<!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>Using Message or Resource Bundle in JSF</title>
</head>
<body>
	<f:view>
		<f:loadBundle var="msgs"
			basename="com.sitenol.resourceBundle.messageBundle" />
		<h:panelGrid columns="1" border="0">
			<h:outputText value="#{msgs.label_greeting}"
				style="text-color : red;"></h:outputText>
			<h:panelGroup>
				<h:outputText value="#{msgs.label_current_year}"></h:outputText>
			</h:panelGroup>
			<h:outputText value="#{msgs.label_java_j2ee_technologies}"></h:outputText>
		</h:panelGrid>
	</f:view>
</body>
</html>

3. Create faces-config.xml file :
Please find the faces-config.xml file code here. This faces-config.xml file is for example using .properties file in JSF.

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_1_2.xsd"
	version="1.2">
</faces-config>

4. Create web.xml file :
Please find the web.xml file code here. This web.xml file is for example using .properties file in JSF.

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>UsingPropertiesFileInJSF</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
  </servlet-mapping>
  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
  </context-param>
  <context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>server</param-value>
  </context-param>
  <context-param>
    <description>
	This parameter tells MyFaces if javascript code should be allowed in
	the rendered HTML output.
	If javascript is allowed, command_link anchors will have javascript code
	that submits the corresponding form.
	If javascript is not allowed, the state saving info and nested parameters
	will be added as url parameters.
	Default is 'true'</description>
    <param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name>
    <param-value>true</param-value>
  </context-param>
  <context-param>
    <description>
	If true, rendered HTML code will be formatted, so that it is 'human-readable'
	i.e. additional line separators and whitespace will be written, that do not
	influence the HTML code.
	Default is 'true'</description>
    <param-name>org.apache.myfaces.PRETTY_HTML</param-name>
    <param-value>true</param-value>
  </context-param>
  <context-param>
    <param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name>
    <param-value>false</param-value>
  </context-param>
  <context-param>
    <description>
	If true, a javascript function will be rendered that is able to restore the
	former vertical scroll on every request. Convenient feature if you have pages
	with long lists and you do not want the browser page to always jump to the top
	if you trigger a link or button action that stays on the same page.
	Default is 'false'
</description>
    <param-name>org.apache.myfaces.AUTO_SCROLL</param-name>
    <param-value>true</param-value>
  </context-param>
  <listener>
    <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
  </listener>
</web-app>

5. Create War and Deploy Application to Server :
In this step we will create war with name UsingPropertiesFileInJSF.war . Please deploy this war file to web application server. After the please start the web application server.

6. Run The Application :
After successfully war deployment. Please start the server. After that open Internet Explorer and hit this “http://localhost:8080/UsingPropertiesFileInJSF/usingResourceBundle.faces”.
So now we can see output of message or resource bundle example in JSF.
Out put screen will be like this

Image showing output screen for JSF properties file tutorial
output : using .properties file in JSF

Share this post:

Related Posts

Leave a Comment