日期:2014-05-16 浏览次数:20352 次
ZZ.
Listen and debug JSF lifecycle phases
The JSF lifecycle will be explained and debugged here using the "poor man's debugging" approach with sysout's. We'll also check what happens if you add immediate="true" to the UIInput and UICommand and what happens when a ConverterException and ValidatorException will be thrown.
Well, you probably already know that the JSF lifecycle contains 6 phases:
You can use a PhaseListener to trace the phases of the JSF lifecycle and execute some processes where required. But you can also use a "dummy" PhaseListener to debug the phases to see what is happening in which phase. Here is a basic example of such a LifeCycleListener:
Note: if you don't have a JSF playground environment setup yet, then you may find this tutorial useful as well: JSF tutorial with Eclipse and Tomcat.
package mypackage; import javax.faces.event.PhaseEvent; import javax.faces.event.PhaseId; import javax.faces.event.PhaseListener; public class LifeCycleListener implements PhaseListener { public PhaseId getPhaseId() { return PhaseId.ANY_PHASE; } public void beforePhase(PhaseEvent event) { System.out.println("START PHASE " + event.getPhaseId()); } public void afterPhase(PhaseEvent event) { System.out.println("END PHASE " + event.getPhaseId()); } }
?
Add the following lines to the faces-config.xml to activate the LifeCycleListener.
<lifecycle> <phase-listener>mypackage.LifeCycleListener</phase-listener> </lifecycle>
?
This produces like the following in the system output:
START PHASE RESTORE_VIEW 1
END PHASE RESTORE_VIEW
1
START PHASE APPLY_REQUEST_VALUES 2
END PHASE APPLY_REQUEST_VALUES
2
START PHASE PROCESS_VALIDATIONS 3
END PHASE PROCESS_VALIDATIONS
3
START PHASE UPDATE_MODEL_VALUES 4
END PHASE UPDATE_MODEL_VALUES
4
START PHASE INVOKE_APPLICATION 5
END PHASE INVOKE_APPLICATION 5
START
PHASE RENDER_RESPONSE 6
END PHASE RENDER_RESPONSE 6
?
To trace all phases of the JSF lifecycle, here is some sample code which represents simple JSF form with a "dummy" converter and validator and the appropriate backing bean. The code sample can be used to give us more insights into the phases of the JSF lifecycle, to understand it and to learn about it.
The minimal contents of the test JSF file: test.jsp
?
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %> <%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <f:view> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Debug JSF lifecycle</title> </head> <body> <h:form> <h:inputText binding="#{myBean.inputComponent}" value="#{myBean.inputValue}" valueChangeListener="#{myBean.inputChanged}"> <f:converter converterId="myConverter" /> <f: