The request processing life-cycle is the most important element of the JSF framework and is a must to understand to get a better grip of JSF. Whenever you do any request to any web application, the request has to be processed in some way by the server and should generate a response back to you. For example when you are doing an online booking than you send your details as part of request. The details are parsed and handled by the server and in turn will give you back a response with confirmed booking details if it is successful. In case of failure, the response outlining the reason of failure is returned back to you.
JSF lifecycle also does the same thing when it has to handle request and generate a response. However JSF framework has come up with a standard way of handling this request response cycle. There are six phases in this lifecycle, each one having a fixed role in the lifecycle. The lifecycle will do the following tasks on the way:
- Processing of request on the server.
- Managing the components on the server side.
- Synchronizing the server and client side UI component.
- Handling navigation to go from one page to another.
- Creating the response.
JSF has following six phases which we will look into in the context of the example in Introduction to JSF.
- Create/Restore View
- Apply Request Values
- Process Validations
- Update Model Values
- Invoke Application
- Render Response
Create/Restore View
Create/Restore view phase is responsible for creating or restoring the server side component tree representing the page on the client side. The view is stored in a parent container object called FacesContext. The servlet programming model guarentees thread safety. So if you hit the url for /faces/jsp/actor.jsp first time, the request is a first request for the actor.jsp page. The /faces/* url pattern brings FacesServlet into picture. FacesServlet traps the request, stritps the /faces part and looks for a jsp page at /jsp/actor.jsp. Though it is a JSP page but notice that no JSP compiler is involved. Rather the page is read by JSF and a component tree is built. If the request comes for second time the same component tree is restored. If the request is a first request the lifecycle directly goes to Render Response phase otherwise it goes to next phase which is Apply Request Values phase.
Apply Request Values
In apply request value phase, the incoming request is processed. The parameter map is fetched and parsed for name value pair. Only UI component who have an input value participate in this phase. So if we look into actor.jsp, in this phase the value contained in the input box will be set into the server side UIComponent representing the input box. Also the click of the submit button is noted in this phase.
Process Validations
In this phase both conversion and validations happen. Conversion is done first followed by validation. For example if we have Date property in our actor bean, than the conversion of string value received from front end is converted to a Java object at this stage. If we put validations of say length on the input box than those validations are checked here. This is a server side validation and has nothing to do with if you put a javascript validation on the front end.
Update Model Values
This is the place where backing beans are populated. So the actor bean will get the name value set by JSF at this stage by the value entered at front end.
Invoke Application
Here the actions or actions listeners are handled. In this case we have clicked a submit button which generated an action "save". The save in this case results in navigation from actor.jsp page to confirm.jsp page as can be seen in the navigation block of faces-config.xml.
Render Response
In the render response phase the response is created. In the example, the view of confirm.jsp will be created and based on the component tree, the html markup is created and the response is send back to the client.
It is possible to short circuit the lifecycle. For example in case of conversion and validation failures, the cycle short circuit to Render response phase directly.
Java Server Faces (JSF) Introduction
Java Server Faces (JSF) Introduction
No comments:
Post a Comment