JSF Lifecycle Diagram If immediate=“true” then Actions, Action Listeners, and Value Change Listeners fire here RESTORE_VIEW APPLY_REQUEST_VALUES HTTP POST / Faces Apply Parse Build Process Process Partial Submit Request Saved State PDL Comp Tree Decodes Events (Post Only) Value Change Listeners Fire Here PROCESS_VALIDATIONS Facelet View Markup UIViewRoot Component <f:view> Process Process HtmlForm Tree <h:form> Validators Events HTTP <h:outputLabel /> GET HtmlOutputLabel <h:inputText /> <h:inputText /> <h:commandButton /> HtmlInputText </h:form> Conversion/Validation </f:view> HtmlCommandButton Success RENDER_RESPONSE INVOKE_APPLICATION UPDATE_MODEL_VALUES Faces Save Process Process Process Process Process Response Comp State Renderers Events Application Events Updates Conversion/Validation Failure Actions & Action Listeners fire here www.icefaces.org ICESOFT TECHNOLOGIES INC.
JSF Lifecycle • The framework defines a lifecycle that executes in distinct phases: – Restore View – Apply Request Values – Process Validations – Update Model Values – Update Model Values – Invoke Application – Render Response www.icefaces.org ICESOFT TECHNOLOGIES INC.
JSF Lifecycle Phase: Restore View RESTORE_VIEW Apply Parse Build Saved State PDL Comp Tree (Post Only) Facelet View Markup UIViewRoot Component <f:view> HtmlForm Tree <h:form> <h:outputLabel /> HtmlOutputLabel <h:inputText /> <h:inputText /> <h:commandButton /> HtmlInputText </h:form> </f:view> HtmlCommandButton • The Restore View phase restores an existing view from the previous request, or creates a new view for an initial request • The resulting view is put into the current FacesContext www.icefaces.org ICESOFT TECHNOLOGIES INC.
JSF Lifecycle Diagram (HTTP GET) RESTORE_VIEW Faces Parse Build Request PDL Comp Tree (GET Only) Facelet View Markup UIViewRoot Component <f:view> HtmlForm Tree <h:form> HTTP <h:outputLabel /> GET HtmlOutputLabel <h:inputText /> <h:inputText /> <h:commandButton /> HtmlInputText </h:form> </f:view> HtmlCommandButton RENDER_RESPONSE Faces Save Process Response Comp State Renderers www.icefaces.org ICESOFT TECHNOLOGIES INC.
JSF Lifecycle Phase: Render Response • The Render Response phase is responsible for – Invoking component renderers for the appropriate client device – Sending a fully encoded response back to the client – The response is typically HTML markup that is written to the HttpServletResponse OutputStream Rendered HTML for Web Browser Rendered HTML for Web Browser <html id="document:html"> RENDER_RESPONSE <body id="document:body"> <form id="form1"> Save Process Comp State Renderers <label for="firstName" /> <input id="firstName" /> <input type="submit" /> </form> </body> www.icefaces.org ICESOFT TECHNOLOGIES INC.
JSF Lifecycle Diagram (HTTP POST – Valid Values) RESTORE_VIEW APPLY_REQUEST_VALUES HTTP POST / Faces Apply Build Process Process Partial Submit Request Saved State Comp Tree Decodes Events (POST Only) PROCESS_VALIDATIONS UIViewRoot Component Process Process HtmlForm Tree Validators Events HtmlOutputLabel HtmlInputText Conversion/Validation HtmlCommandButton Success RENDER_RESPONSE INVOKE_APPLICATION UPDATE_MODEL_VALUES Faces Save Process Process Process Process Process Response Comp State Renderers Events Application Events Updates www.icefaces.org ICESOFT TECHNOLOGIES INC.
JSF Lifecycle Phase: Apply Request Values • The Apply Request Values phase is APPLY_REQUEST_VALUES where submitted request parameters are Process Process mapped to their corresponding UIInput Decodes Events components UIViewRoot Component Request HtmlForm Tree Parameter Values HtmlOutputLabel HtmlInputText HtmlCommandButton // Process decodes does something // kind of like this for each // EditableValueHolder: String firstName = request.getParameter(“firstName”); UIInput.setSubmittedValue(firstName); www.icefaces.org ICESOFT TECHNOLOGIES INC.
JSF Lifecycle Phase: Process Validations • The Process Validations phase is where submitted values are processed by JSF converters and validators PROCESS_VALIDATIONS UIViewRoot Component Process Process HtmlForm Tree Validators Events HtmlOutputLabel HtmlInputText // If validation passes: HtmlCommandButton UIInput.setValid(true); // Otherwise: UIInput.setValid(false); • If any component fails validation, then – The component’s valid property is set to false – A FacesMessage is added to the FacesContext www.icefaces.org ICESOFT TECHNOLOGIES INC.
JSF Lifecycle Phase: Update Model Values • If conversion & validation passes, then the Update Model Values phase is reached • If EL value bindings were specified on UIInput components, then those component values are set in the model • For example, the following EL value binding: <h:inputText value=“#{MyBean.firstName}”/> Conversion/Validation Success … will cause the MyBean.setFirstName() UPDATE_MODEL_VALUES method to be called and will pass it the Process Process result of HtmlInputText.getValue() Events Updates www.icefaces.org ICESOFT TECHNOLOGIES INC.
JSF Lifecycle Diagram (HTTP POST – Invalid Values) RESTORE_VIEW APPLY_REQUEST_VALUES HTTP POST / Faces Apply Build Process Process Partial Submit Request Saved State Comp Tree Decodes Events (POST Only) PROCESS_VALIDATIONS UIViewRoot Component Process Process HtmlForm Tree Validators Events HtmlOutputLabel HtmlInputText HtmlCommandButton RENDER_RESPONSE INVOKE_APPLICATION UPDATE_MODEL_VALUES Faces Save Process Process Process Process Process Response Comp State Renderers Events Application Events Updates Conversion/Validation Failure www.icefaces.org ICESOFT TECHNOLOGIES INC.
JSF Lifecycle Phase: Invoke Application • The Invoke Application phase is responsible for reacting to the submitted values by invoking methods in managed- beans that perform some kind of action • ActionListener methods are called first, then Actions • Markup Examples: <h:commandButton actionListener = “#{MyBean.checkAll}” /> INVOKE_APPLICATION Process Process Events Application <h:commandButton action =“#{MyBean.submit}” /> www.icefaces.org ICESOFT TECHNOLOGIES INC.
JSF Lifecycle Diagram: Events If immediate=“true” then Actions, Action Listeners, and Value Change Listeners fire here RESTORE_VIEW APPLY_REQUEST_VALUES HTTP POST / Faces Apply Parse Build Process Process Partial Submit Request Saved State PDL Comp Tree Decodes Events (Post Only) Value Change Listeners Fire PROCESS_VALIDATIONS Here Process Process Validators Events Conversion/Validation Success RENDER_RESPONSE INVOKE_APPLICATION UPDATE_MODEL_VALUES Faces Save Process Process Process Process Process Response Comp State Renderers Events Application Events Updates Actions & Action In JSF 1.x, there Listeners fire are no events here that fire here www.icefaces.org ICESOFT TECHNOLOGIES INC.
Phase Listeners • PhaseListeners can be extremely helpful for application customization, optimization, and debugging • Must implement the following interface: – javax.faces.application.PhaseListener • Use javax.faces.event.PhaseId to specify which phase to listen for • Register implementing class in the faces- config.xml <lifecycle> <phase-listener> training.jobApplication.lifecycle.LoggingPhaseListener </phase-listener> </lifecycle> www.icefaces.org ICESOFT TECHNOLOGIES INC.
Exercise: Overview • The goal of this exercise is to add a PhaseListener to the jobApplication project • The PhaseListener will simply log the Phase ID to the Tomcat console log, before and after phases execute www.icefaces.org ICESOFT TECHNOLOGIES INC.
Step 1: Create LoggingPhaseListener.java LoggingPhaseListener www.icefaces.org ICESOFT TECHNOLOGIES INC.
Step 2: Paste Java Code • For the sake of convenience, open the LoggingPhaseListener.java file in the Eclipse Java Editor and paste the following code as the body of the LoggingPhaseListener class: private static final long serialVersionUID = 5805974145765679633L; private static final Log log = LogFactory.getLog(LoggingPhaseListener.class); public void afterPhase(PhaseEvent phaseEvent) { if (log.isInfoEnabled()) { log.info("AFTER PHASE: " + phaseEvent.getPhaseId().toString()); } } public void beforePhase(PhaseEvent phaseEvent) { if (log.isInfoEnabled()) { log.info("BEFORE PHASE: " + phaseEvent.getPhaseId().toString()); } } public PhaseId getPhaseId() { return PhaseId.ANY_PHASE; } www.icefaces.org ICESOFT TECHNOLOGIES INC.
Step 3: Paste XML • Paste the following XML fragment into the faces-config.xml file: <lifecycle> <phase-listener> training.jobapplication.lifecycle.LoggingPhaseListener </phase-listener> </lifecycle> • This fragment will cause the LoggingPhaseListener class to be registered as a PhaseListener when the JSF framework initializes www.icefaces.org ICESOFT TECHNOLOGIES INC.
Recommend
More recommend