Docstoc

JSFLifeCycle

Document Sample
JSFLifeCycle Powered By Docstoc
					Request Processing Life Cycle
1

Topics
● ●

●

Request life cycle Life cycle of components with “immediate” attribute PhaseListener

2

Request Life Cycle as 6 Phases
3

Request Life cycle
● ●

●

●

A JSF page is represented by a tree of UI components, called a view When a client makes a request for the page, the life-cycle starts During the life-cycle, JSF implementation must build the view of a page while considering state saved from the previous postback When the client performs a postback of the page, JSF implementation must perform life-cycle steps
– –

validation conversion
4

Request Processing Lifecycle
Response Complete Response Complete

Faces Request

Reconstitute Component Tree

Apply Request Values Render Response Response Complete

Process Events

Process Validations

Process Events

Response Complete

Faces Response

Render Responder

Process Events

Invoke Application

Process Events

Update Model Values

Conversion Errors / Render Response Validation / Conversion Errors / Render Response

5

Request Processing
●

Life-cycle handles two types of requests
–

Initial request & Postback A user requests the page for the first time Lifecycle only executes the “restore view” and “render response” phases A user submits the form contained on a page that was previously loaded into the browser as a result of executing an initial request Lifecycle executes all phases
6

●

Initial request
– –

●

Postback
–

–

Request Processing Lifecycle Phases
1.Restore component tree phase 2.Apply request values phase 3.Process validations phase 4.Update model values phase 5.Invoke application phase 6.Render response phase

7

greeting.jsp (from guessNumer)
<HTML> <HEAD> <title>Hello</title> </HEAD> <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> <body bgcolor="white"> <f:view> <h:form id="helloForm" > <h2>Hi. My name is Duke. I'm thinking of a number from <h:outputText value="#{UserNumberBean.minimum}"/> to <h:outputText value="#{UserNumberBean.maximum}"/>. Can you guessit?</h2> <h:graphicImage id="waveImg" url="/wave.med.gif" /> <h:inputText id="userNo" value="#{UserNumberBean.userNumber}"> <f:validateLongRange minimum="0" maximum="10" /> </h:inputText> <h:commandButton id="submit" action="success" value="Submit" /> <p> <h:message style="color: red; font-family: 'New Century Schoolbook', serif; font-style: oblique; text-decoration: overline" id="errors1" for="userNo"/> </h:form> </f:view> </HTML> 8

Phase 1: Restore Component Tree Phase
9

Phase 1: Restore Component Tree (View) Phase
● ●

●

A request comes through the FacesServlet controller The controller examines the request and extracts the view ID, which is determined by the name of the JSP page. The controller uses the view ID to look up the components for the current view. The view contains all the GUI components.
– –

If the view doesn't already exist, the JSF controller creates it (occurs at initial request) If the view already exists, the JSF controller uses it (occurs at postback) 10

Phase 1: Restore Component Tree (View) Phase
●

When a request for a JavaServer Faces is made, such as when clicking on a link or a button, the JSF implementation begins the Restore view phase
– – –

Builds or finds the view of the page Wires up event handlers and validators (to each UI component) Saves the view in the FacesContext

11

Example: Component Tree (View) of greeting.jsp page of GuessNumber

UIView helloForm userNo submit errors1

12

Phase 2: Apply Request Values Phase
13

Phase 2: Apply Request Values Phase
●

●

Each ValueHolderType component in the tree extracts its new value from the request parameters with its built-in decode method Within the decode method, the value is stored locally on the component
– – –

submitedValue field of the component For userNo component in greeting.jsp page, type gets converted from String to Integer Conversion errors are queued on the FaceContext
14

Phase 3: Process Validation Phase
15

Phase 3: Process Validation Phase
●

JSF implementation processes all input validations registered on the components in the tree
–

This is input validation (not business logic validation) Error messages are queued in FacesContext Lifecycle advances directly to the Render Response phase userNo has be be between 1 and 10
16

●

In case of validation errors
– –

●

Example
–

Phase 4: Update Model Values Phase
17

Phase 4: Update Model Values Phase
●

JSF implementation walks the component tree and set the corresponding server side object properties to the components' local values
– –

Update the bean properties pointed at by an input component's value attribute Type conversion from local value to model property type userNumber property of the UserNumberBean is set to the local value of the userNo component
18

●

Example in greeting.jsp
–

Phase 5: Invoke Application Phase
19

Phase 5: Invoke Application Phase
●

JSF implementation handles any application-level events, such as submitting a form or linking to another page

20

Phase 5: Example in GuessNumber
●

●

●

●

●

The greeting.jsp page from the guessNumber example has one application-level event associated with the UICommand component A default ActionListener implementation retrieves the outcome, "success", from the component's action attribute The listener passes the outcome to the default NavigationHandler The NavigationHandler matches the outcome to the proper navigation rule defined in the application's application configuration file to determine what page needs to be displayed next JSF implementation then sets the response view to that of the new page
21

Phase 6: Render Response Phase
22

Phase 6: Render Response Phase
●

JSF implementation invokes the components' built-in encode method and renders the components from the component tree saved in the FacesContext
– –

Create appropriate markup from component tree If errors in previous phases, original page is rendered with any queued error messages

●

State of the response is saved so that subsequent requests can access it and it is available to the Restore View phase
23

PhaseListener
24

PhaseListener
●

●

●

An interface implemented by objects that wish to be notified at the beginning and ending of processing for each standard phase of the request processing lifecycle You can provide your own implementation of PhaseListener and plug it into the application for custom request handling – Ajax request handling Before and after each phase handling – “around” semantics
25

PhaseListener Configuration
●

Configuring PhaseListener class in the facesconfig.xml file
<faces-config> <lifecycle> <phase-listener>tracker.PhaseTracker</phase-listener> </lifecycle> ...

26

Questions?

27