Java Server Faces

15
Java Server Faces Bayu Priyambadha, S.Kom

Transcript of Java Server Faces

Page 1: Java Server Faces

Java Server FacesBayu Priyambadha, S.Kom

Page 2: Java Server Faces

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)

Page 3: Java Server Faces

The Advantages Code can be reused and extended for

components through the templating andcomposite 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.

Page 4: Java Server Faces

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)

Backing Bean(java)

Logic Layer(rest of app)

faces-config(xml)

Wrapper(java)

model(java)

Page 5: Java Server Faces

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>

Page 6: Java Server Faces

JSF Architecture

Page 7: Java Server Faces

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 thefaces-config.xml file and give the application developer full access to all the mapped backing bean’s methods

Page 8: Java Server Faces

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>

Page 9: Java Server Faces

Model (bean scope)Managed Bean Scope DescriptionNone Instance created for every

method invocationRequest Instance created for every

requestSession Instance created on initial

request and stored in the sessionApplication Instance created on initial

request and stored in the Web application

Page 10: Java Server Faces

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

Page 11: Java Server Faces

View (UIComponents)

Page 12: Java Server Faces

Controller JSF comes with a simple controller—the

FacesServlet The FacesServlet acts as a gatekeeper,

controlling navigation flow and ispatching requests to the appropriate JSF page

javax.faces.webapp.FacesServlet

Page 13: Java Server Faces

Example (Create Bean)package hello;

import javax.faces.bean.ManagedBean;

@ManagedBean

public class Hello {

final String world = "Hello World!";

public String getworld() {

return world;

}

}

Page 14: Java Server Faces

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>

Page 15: Java Server Faces