JSF 2 f view

JSF 2 f view


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

In this tutorial you will learn about the JSF 2 core tag view.

JSF 2 f view

In this tutorial you will learn about the JSF 2 core tag view.

<f:view> tag is acted as a container. On the JSF pages it contains all the core and custom component actions of JavaServer Faces.

Attributes of <f:view> tag

  • locale : This attribute is not required to use with this tag. It is used to specified the locale to localize the current page. This attribute only evaluates the java.util.Locale or a java.lang.String which is converted to a Locale.
  • renderKitId : This attribute is not required to use with this tag. It specifies the identifier of RenderKit which uses to render the current page.
  • beforePhase : This attribute is not required to use with this tag. It binds the method which have the javax.faces.event.PhaseEvent and have the return type void. Excluding the restore view the bounded method will be called before every phase.
  • afterPhase : This attribute is not required to use with this tag. It binds the method which have the javax.faces.event.PhaseEvent and have the return type void. Excluding the restore view the bounded method will be called after every phase.

Example :

Here I am giving a simple example which will demonstrate you about how to use the JSF 2 core tag view. As we know this tag is acted as a container so you can use all the JavaServer Faces core and custom component actions inside it. Except this using the attributes of this tag we can do specific task also. In this example we are using the locale attribute to change the Locale of current page. For this at first, I have created the two properties files which will be separately used to show the specific Locale. Then I have created a JavaBean class with property 'name' and its setter getter method. This bean will be used to find out the <h:inputText> value. When you will change the value of 'locale' attribute you will saw changes in the data according to the specified properties file.

Directory Structure

FormBean.java

package devmanuals;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ApplicationScoped;

@ManagedBean(name="formBean")
@ApplicationScoped
public class FormBean {

private String name;

public String getName() {
return name;
}

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

dev_en.properties

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

'en' is the English locale.

dev_de.properties

text1=Geben Sie Ihren Namen : 
text2=Dein Name ist : {0}
button=einreichen

'de' is the German locale

locale.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">
<head></head>
<body>
<f:view locale="en">
<h:form>
<h:outputText value="#{loc.text1}"/> 
<h:inputText value="#{formBean.name}" />
<h:commandButton value="#{loc.button}" /> <br/><br/>
</h:form>
<h:panelGrid>
<h:outputFormat value="#{loc.text2}">
<f:param value="#{formBean.name}" />
</h:outputFormat>
</h:panelGrid>
</f:view>
</body>
</html>

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>devmanuals.Property.dev</base-name>
<var>loc</var>
</resource-bundle>
</application>

</faces-config>

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>jsfView</display-name>
<welcome-file-list>
<welcome-file>locale.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

To run this example download the WAR file by clicking the link Download Source Code and import it as WAR file into your eclipse or by copying the above code and paste them into the respective files according to the directory structure and then start your web server and use the following link http://localhost:8181/jsfView/locale.jsf into the address bar of your web browser.

Output :

When you will execute the above code you will get the output as follows :

1. When you will set the locale in the <f:view locale="en"> tag then the output will be as follows :

2. When you will set the locale in the <f:view locale="de"> tag then the output will be as follows :

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics