Understanding the Request Processing Lifecycle Phases - Java
This chapter gently introduces the JavaServer Faces technology. More importantly, it teaches you how to write your first JSF application to get a feel for how this great technology works. In addition to the sample chapters, this chapter prepares you for the next chapters by introducing the JSF Application Programming Interface (API) and the Application Configuration file. This excerpt comes from chapter two of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983-7, 2004).
The Lifecycle object processes a JSF request (encapsulated in the FacesContext object; the FacesContext is the object that is read and modified by Lifecycle during processing) in six phases, executed in the following order:
Reconstitute Component Tree A JSP page in a JSF application is represented by a component tree. This phase starts the Lifecycle request processing by constructing this tree. Each component tree has an identifier that is unique throughout the application. The identifier of a component tree is the path information portion of the request URI. For a request with the URI /faces/index.jsp, for instance, the tree identifier is /index.jsp. The constructed component tree is then saved in the FacesContext object for processing by the following request processing phases.
Apply Request Values In this phase, the local value of each component in the component tree is updated from the current request. A value can come from a request parameter, a header, a cookie, and so on. During this phase, a component may queue events. These events will be processed during the process event steps in the request processing lifecycle.
Process Validations After the local value of each component is updated, in the Process Validations phase, the Lifecycle object will validate those values if necessary. A component that requires validation must provide implementation of the validation logic. Alternatively, a JSF programmer can register zero or more validators with the component. If one or more external validators are found, the local value of each component will be validated using the validation logic in these external validators.
Update Model Values This phase can be reached only if the local values of all components in the tree are valid. In this phase, the Lifecycle object updates the application’s model data. During this phase, a component may again queue events.
Invoke Application During this phase, the JSF implementation handles any application level events, such as submitting a form or linking to another page.
Render Response In this phase, the JSF implementation renders the response to the client.
The Apply Request Values, Process Validations, Update Model Values, and Invoke Application phases in the request processing lifecycle may queue events in the FacesContext instance associated with the current request. Therefore, the JSF implementation must handle these events after these phases.
Between two phases, the Lifecycle object checks any event listener that needs to be called. When writing an event listener, you can choose after which phase the listener should be executed. Alternatively, you can write an event listener that is called after various phases.
Figure 3 illustrates the processing of a JSF request through these phases. The smaller boxes that are labeled “Process Events” indicate the steps that the Lifecycle object takes to execute event listeners.
Figure 3The phases of the request processing lifecycle
Note that an event listener can change the course of the processing flow by indicating to the Lifecycle object that the processing should jump to the last phase or be terminated immediately after the current event processing.
Remember: This is part one of the second chapter of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983). Stay tuned for part 2 of "Introduction to JavaServer Faces (JSF)," where we learn about JSP, JavaBeans, and Model 2. Buy this book!