JSF 2 f loadBundle Tag

JSF 2 f loadBundle Tag


Posted in : Java Posted on : June 7, 2012 at 7:15 PM Comments : [ 0 ]

In this tutorial you will learn about how to load resource bundle locally in JSF.

JSF 2 f loadBundle Tag

In this tutorial you will learn about how to load resource bundle locally in JSF.

<f:loadBundle> tag is used to load the resource bundle locally in the JSF page. Resource bundle is a Java .properties file where the values are kept in as key-value pair. Once a resource bundle is loaded, it can be accessed as the java.util.Map in the current request attributes of the current request through the value of 'var' attribute of this tag. Benefit to use loadBundle is the developers are not required to hard-code the messages into the Java pages directly rather they can write the messages into the .properties files and can load using loadBundle tag. However the .properties file can also be loaded globally (How ? Click Here).

Attributes of <f:loadBundle> tag

  • basename : This is not a required attribute but, is used to specify the base name of the required resource bundle (.properties file).
  • var : This is a required attribute which specifies the request scope attribute's name, inside which the resource bundle is accessed as Map.

Example :

Here an example is being given which will demonstrate you about how to use the <f:loadBundle> tag to load the resource bundle for displaying messages. In this example a Properties file has been created for reading the messages on/through the JSF pages. To do so at first I have created a New Dynamic Web Project and added the JSF capabilities into it. Then created a .properties file inside the package in the src folder in Eclipse to use the messages onto the JSF page also created a Java Bean class within this package with data member 'name' and its setter method to set the value given into the input textbox and the getter method to find the value of input textbox then created a folder for containing the JSF pages named jsfPages in the Web Content folder in Eclipse and finally changed the welcome-file list in the auto generated web.xml file in this application as <welcome-file>jsfPages/inputName.xhtml</welcome-file>.

Directory Structure

prop.properties

text1=Enter your Name : 
text2=Your Name is : {0}
button=SUBMIT

NameBean.java

package devmanuals;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name="nameBean")
@RequestScoped
public class NameBean {

String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
} 
}

inputName.xhtml

<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">

<f:loadBundle var="pr" basename="devmanuals.prop" />
<f:view>
<h:form>
<h:outputText value="#{pr.text1}"/><h:inputText value="#{nameBean.name}" />
<h:commandButton value="#{pr.button}" /> <br/><br/>
</h:form>
<h:panelGrid>
<h:outputFormat value="#{pr.text2}">
<f:param value="#{nameBean.name}" />
</h:outputFormat>
</h:panelGrid>
</f:view>
</html>

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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>jsfLoadBundle</display-name>
<welcome-file-list>
<welcome-file>jsfPages/inputName.xhtml</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>
<url-pattern>*.jsf</url-pattern>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<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>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
</web-app>

How to Run

Start/run your server then use the following URL into the address bar of your web browser http://localhost:8080/jsfLoadBundle/jsfPages/inputName.jsf

Output :

When you will execute this example first time then the output will be as follows :

Now when you will give any value into the textbox and clicked on submit button then the output will be as follows :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics