JSF新手可能常常听到 "JSF Request Processing Lifecycle" ,但是不一定知道在生命周期中都发生了什么事情.
这里有一点代码, 一个 PhaseListener , 我们可以用她来了解 JSF's Request Processing Lifecycle如何工作的.
下面是一个在生命周期开始和结束时执行的一个简单的PhaseListener
package com.jsf; import javax.faces.event.PhaseListener; import javax.faces.event.PhaseEvent; import javax.faces.event.PhaseId; public class MyPhaseListener implements PhaseListener { public MyPhaseListener() { } public void beforePhase(PhaseEvent pe) { if (pe.getPhaseId() == PhaseId.RESTORE_VIEW) System.out.println("Processing new Request!"); System.out.println("before - " + pe.getPhaseId().toString()); } public void afterPhase(PhaseEvent pe) { System.out.println("after - " + pe.getPhaseId().toString()); if (pe.getPhaseId() == PhaseId.RENDER_RESPONSE) System.out.println("Done with Request!\n"); } public PhaseId getPhaseId() { return PhaseId.ANY_PHASE; } }
当每一个阶段开始和结束的时候,她 在控制台上打印出一些东西. 你需要在 faces-config.xml中来注册该listener
<lifecycle> <phase-listener>com.jsf.MyPhaseListener /> </lifecycle>
一旦你注册了 PhaseListener, 你就可以开始写个小程序来测试了. 我们使用一个简单的程序,一个输入表单和按钮:
<h:commandButton value="button" action="#{backingbean.button_action}"/<
按钮绑定了一个manage bean的动作方法.:
public String button_action() { System.out.println("Action event processed..."); return "success"; }
运行该页面可以看到下面的输出,.:
05/08/31 20:32:46 Processing new Request! 05/08/31 20:32:46 before - RESTORE_VIEW 1 05/08/31 20:32:46 after - RESTORE_VIEW 1 05/08/31 20:32:46 before - RENDER_RESPONSE 6 05/08/31 20:32:46 after - RENDER_RESPONSE 6 05/08/31 20:32:46 Done with Request! 05/08/31 20:32:57 Processing new Request! 05/08/31 20:32:57 before - RESTORE_VIEW 1 05/08/31 20:32:57 after - RESTORE_VIEW 1 05/08/31 20:32:57 before - APPLY_REQUEST_VALUES 2 05/08/31 20:32:57 after - APPLY_REQUEST_VALUES 2 05/08/31 20:32:57 before - PROCESS_VALIDATIONS 3 05/08/31 20:32:57 after - PROCESS_VALIDATIONS 3 05/08/31 20:32:57 before - UPDATE_MODEL_VALUES 4 05/08/31 20:32:57 after - UPDATE_MODEL_VALUES 4 05/08/31 20:32:57 before - INVOKE_APPLICATION 5 05/08/31 20:32:57 Action event processed... 05/08/31 20:32:57 after - INVOKE_APPLICATION 5 05/08/31 20:32:57 before - RENDER_RESPONSE 6 05/08/31 20:32:57 after - RENDER_RESPONSE 6 05/08/31 20:32:57 Done with Request!
注意在第一个请求 (non-postback) ,只有两个阶段, RESTORE_VIEW and RENDER_RESPONSE..这说明她只是创建 UI component tree然后显示她 . 下一步,你点击按钮,注意打印的消息. 这次她通过了整个jsf生命周期. 注意,按钮点击的动作事件在 INVOKE_APPLICATION phase处理...
现在,作为一个实验,把 commandButton 的 immediate 属性改变为 true ,看看如何影响动作事件的处理的.
下一步你可能想添加一个输入表单,并且添加一个 ValueChangeListener 来显示一个值变化事件.
A new input field...
<h:inputText value="foo"> <f:valueChangeListener type="com.jsf.MyValueChangeListener"/> </h:inputText>
下面是ValueChangeListener代码..
package com.jsf; import javax.faces.event.ValueChangeListener; import javax.faces.event.ValueChangeEvent; import javax.faces.event.AbortProcessingException; public class MyValueChangeListener implements ValueChangeListener { public MyValueChangeListener() { } public void processValueChange(ValueChangeEvent vce) throws AbortProcessingException { System.out.println("A value has changed!"); } }
这里你不需要在faces配置文件中注册 ValueChangeListener,你可以直接运行程序,看看有什么信息打印出来.
05/08/31 20:41:33 Processing new Request! 05/08/31 20:41:33 before - RESTORE_VIEW 1 05/08/31 20:41:33 after - RESTORE_VIEW 1 05/08/31 20:41:33 before - RENDER_RESPONSE 6 05/08/31 20:41:33 after - RENDER_RESPONSE 6 05/08/31 20:41:33 Done with Request!
输入一个值在输入表单中, 看看有什么发生.
5/08/31 20:42:42 Processing new Request! 05/08/31 20:42:42 before - RESTORE_VIEW 1 05/08/31 20:42:42 after - RESTORE_VIEW 1 05/08/31 20:42:42 before - APPLY_REQUEST_VALUES 2 05/08/31 20:42:42 after - APPLY_REQUEST_VALUES 2 05/08/31 20:42:42 before - PROCESS_VALIDATIONS 3 05/08/31 20:42:42 A value has changed! 05/08/31 20:42:42 after - PROCESS_VALIDATIONS 3 05/08/31 20:42:42 before - UPDATE_MODEL_VALUES 4 05/08/31 20:42:42 after - UPDATE_MODEL_VALUES 4 05/08/31 20:42:42 before - INVOKE_APPLICATION 5 05/08/31 20:42:42 Action event processed! 05/08/31 20:42:42 after - INVOKE_APPLICATION 5 05/08/31 20:42:42 before - RENDER_RESPONSE 6 05/08/31 20:42:42 after - RENDER_RESPONSE 6 05/08/31 20:42:42 Done with Request! ref:http://hi.baidu.com/jsfcn/blog/item/17b86506bd682e7b030881ca.html