Java Server Faces
Java Server Faces
(JSF)
Java Server Faces (JSF)
• Restore View:
– The Restore View is the first phase of JSF life cycle.
– When a request comes, it create a component tree and
save it into FacesContext instance. If it is already
created then, restore view retrieves the component
tree for the requested page from FacesContext
instance.
– It also connect handler and validators to the
components
• Note: If there is no query data then render
response phase will execute directly and
intermediate phase will not execute.
JSF Life Cycle
• Process Validations:
– In this phase, all convertors and validators are executed
on local values and if validation passes then, all other
phases executes normally otherwise JSF
implementation adds an error message to the
FacesContext instance and render response phase
executes directly.
JSF Life Cycle
• Bean Scopes:
– When we define a bean, we have to define its scope
also in which it will be placed. JSF implementation
provide us several scope for a bean. Request scope is
the default scope.
• Commonly used bean scopes:
– Request scope.
– View scope.
– Session scope.
– Application scope.
JSF Beans Scope
• Request scope:
– In request scope, bean created when a request involving this
bean comes and destroyed after completing response.
• View scope:
– In view scope, bean created when a request involving this bean
comes and destroyed when view changed.
• Session Scope:
– In session scope bean created when first request involving this
bean comes and destroyed when session is terminated.
• Application scope:
– In application scope, bean created when first request involving
this bean comes and remains lives for whole duration of web
application. An application scope managed bean is shared in all
requests and sessions.
JSF Configuration Files
package com.mypack;
/**
* Managed bean.
* @author Anonymous
*/
public class HelloWorld {
public String getMessage() {
return "JSF hello world example.";
}
}
JSF – HelloWorld Example (faces-config.xml)
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head> <title>JSF hello world example.</title>
</h:head>
<h:body>
<h3> <h:outputText value="$
JSF – HelloWorld Example Using Annotations
package com.mypack;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
/**
* Managed bean.
* @author Anonymous
*/
@ManagedBean(name="helloWorld")
@SessionScoped
public class HelloWorld {
public String getMessage() {
return "JSF hello world example using annotation.";
}
}
JSF – HelloWorld Example
JSF – HelloWorld Example
JSF – HelloWorld Example