Java Server Faces

Download Report

Transcript Java Server Faces

Java Server
Faces
Bayu Priyambadha, S.Kom
Java Server Faces




JSF is a UI component framework for J2EE
applications
JSF is a request-driven MVC web framework
for constructing user interfaces using
components (Wiki)
As a display technology, JSF 2 uses Facelets,
an efficient, simple, and powerful view
description language (VDL) (Wiki)
JSF 1.x uses JavaServer Pages (JSP) for its
display technology (Wiki)
The Advantages



Code can be reused and extended for
components through the templating and
composite component features.
When you use the JavaServer Faces Annotations
feature, you can automatically register the
backing bean as a resource available for
JavaServer Faces applications.
Most important, JavaServer Faces technology
provides a rich architecture for managing
component state, processing component data,
validating user input, and handling events.
JSF structure






The template (most
commonly jsp) defines the
interface (in 2.0 using
Facelets)
The faces-config defines the
navigation and the backing
beans
Backing beans handle
action processing,
navigation processing, and
connections to the logic
(business) layer
Wrapper bean wraps the
data POJOs for JSF handling
Logic layer beans can be
injected as defined in the
faces-config
Model is basic data POJO
Template
(jsp)
faces-config
(xml)
Backing Bean
(java)
Wrapper
(java)
Logic Layer
(rest of app)
model
(java)
Navigation (faces-config.xml)
<navigation-rule>
<from-view-id>/index.xhtml</from-view-id>
<navigation-case>
<from-outcome>case1</from-outcome>
<to-view-id>/welcome.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>case2</from-outcome>
<to-view-id>/welcome2.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
JSF Architecture
Model
 With
JSF, the concept of a managed
bean has been introduced
 The managed bean is the glue to the
application logic (backing code or
backing bean)
 Managed beans are defined in the
faces-config.xml file and give the
application developer full access to all
the mapped backing bean’s methods
Model (managed bean)
<managed-bean>
<managed-bean-name>sample</managed-bean-name>
<managed-bean-class>
com.apress.projsf.ch1.application.SampleBean
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
Model (bean scope)
Managed Bean Scope
Description
None
Instance created for every
method invocation
Request
Instance created for every
request
Session
Instance created on initial
request and stored in the session
Application
Instance created on initial
request and stored in the Web
application
View
 The
JSF view layer describes the intended
layout, behavior, and rendering of the
application
 UIComponents are the foundation of the
JSF view layer and represent the behavior
and structure of the application
View (UIComponents)
Controller
 JSF
comes with a simple controller—the
FacesServlet
 The FacesServlet acts as a gatekeeper,
controlling navigation flow and is
patching requests to the appropriate JSF
page
 javax.faces.webapp.FacesServlet
Example (Create Bean)
package hello;
import javax.faces.bean.ManagedBean;
@ManagedBean
public class Hello {
final String world = "Hello World!";
public String getworld() {
return world;
}
}
Example (Create Facelet)
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Facelets Hello World</title>
</h:head>
<h:body>
#{hello.world}
</h:body>
</html>