关于ADF的生命周期请参考《ADF的生命周期介绍》。
1. 定义PagePhaseListener
import javax.faces.event.PhaseId;
import oracle.adf.controller.v2.lifecycle.Lifecycle;
import oracle.adf.controller.v2.lifecycle.PagePhaseEvent;
import oracle.adf.controller.v2.lifecycle.PagePhaseListener;
public class MyPagePhaseListener implements PagePhaseListener {
public MyPagePhaseListener() {
super();
}
public void beforePhase(PagePhaseEvent pe) {
if (pe.getPhaseId() == 9)
System.out.println("###### Processing new Request!");
System.out.println("before - " + pe.getDebugValue() + " " + pe.getPhaseId());
}
public void afterPhase(PagePhaseEvent pe) {
System.out.println("after - " + pe.getDebugValue() + " " + pe.getPhaseId());
//if (pe.getPhaseId() == Lifecycle.PREPARE_RENDER_ID)
if (pe.getPhaseId() == 14)
System.out.println("###### Done with Request!\n");
}
}
注意,这里的参数对象类型是ADE中的PagePhaseEvent,而不是JSF中的PhaseEvent。
在实验中,我发现,JSF的生命周期的各个阶段的Id值分别对应到9至14,与PhaseId中的各个阶段常量值不匹配。
我感觉是一个设计上的缺陷,如果ADF能够在Lifecycle类中重新定义ADF的生命周期的各个阶段就好了。
2. 配置adf-settings.xml
(1)在ViewController项目下建立目录META-INF,实际上就是在ViewController目录下的src目录下建立META-INF目录。
(2)在META-INF目录下创建adf-settings.xml,内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<adf-settings xmlns="http://xmlns.oracle.com/adf/settings">
<adf-controller-config xmlns="http://xmlns.oracle.com/adf/controller/config">
<lifecycle>
<phase-listener>
<listener-id>myPagePhaseListener</listener-id>
<class>view.pagephaselistener.MyPagePhaseListener</class>
</phase-listener>
</lifecycle>
</adf-controller-config>
</adf-settings>
3. 开发一个基于ADF-BC的页面
4. 运行页面
在Server日志中,输出如下结果:
###### Processing new Request!
before - jsfRestoreView 9
after - jsfRestoreView 9
###### Processing new Request!
before - jsfRestoreView 9
after - jsfRestoreView 9
before - initContext 0
after - initContext 0
before - prepareModel 1
after - prepareModel 1
before - jsfRenderResponse 14
before - prepareRender 8
after - prepareRender 8
after - jsfRenderResponse 14
###### Done with Request!
###### Processing new Request!
before - jsfRestoreView 9
after - jsfRestoreView 9
before - initContext 0
after - initContext 0
before - prepareModel 1
after - prepareModel 1
before - jsfApplyRequestValues 10
after - jsfApplyRequestValues 10
before - jsfProcessValidations 11
after - jsfProcessValidations 11
before - jsfUpdateModelValues 12
after - jsfUpdateModelValues 12
before - validateModelUpdates 5
after - validateModelUpdates 5
before - jsfInvokeApplication 13
after - jsfInvokeApplication 13
before - metadataCommit 7
after - metadataCommit 7
before - jsfRenderResponse 14
before - prepareRender 8
after - prepareRender 8
after - jsfRenderResponse 14
###### Done with Request!
可以看出,页面第一次Load时,只经历了两个大的阶段:Restore_View和Render_Response。
点击Submit按钮提交后,经历了全部的阶段。
另外,还注意到,在Render_Response阶段,不是像我们想象的那样先执行jsfRenderResponse,结束后再执行prepareRender。
而是prepareRender阶段“嵌”在jsfRenderResponse中执行。
为了更好的观察,ADF在各个阶段都做了些什么,修改ViewController项目的Debug参数,重新运行页面。
这一次我们可以看出,在不同的阶段ADF执行了很多不同的操作。
###### Processing new Request!
before - jsfRestoreView 9
after - jsfRestoreView 9
before - initContext 0
after - initContext 0
before - prepareModel 1
after - prepareModel 1
before - jsfApplyRequestValues 10
after - jsfApplyRequestValues 10
before - jsfProcessValidations 11
after - jsfProcessValidations 11
before - jsfUpdateModelValues 12
after - jsfUpdateModelValues 12
before - validateModelUpdates 5
after - validateModelUpdates 5
before - jsfInvokeApplication 13
after - jsfInvokeApplication 13
before - metadataCommit 7
after - metadataCommit 7
before - jsfRenderResponse 14
before - prepareRender 8
after - prepareRender 8
after - jsfRenderResponse 14
###### Done with Request!
Project 下载:ADF_Lifecycle.7z
参考文献:
1. https://blogs.oracle.com/jdevotnharvest/entry/how_to_configure_an_adf_phase_listener_and_where_to_put_the_file
2. FOD Demo中的FODPhaseListener类。
3. 《Fusion Developer's Guide for Oracle Application Development
Framework》之How to Set Release Level in an ADF PagePhaseListener
没有评论:
发表评论