运行环境:JDeveloper 11.1.2.2.0 + Oracle Database 10g Express Edition 10.2.0.1。
我们常用的组件是OutputText,它是用来显示Label的。那么,ActiveOutputText比OutputText特殊在哪里呢?
当内容发生改变时,ActiveOutputText组件可以帮助我们从后台“动态刷新”内容,用户无需做任何操作。
同样的功能如果使用OutputText组件来实现,就需要使用PPR的功能。
本实验取材于ADF Faces Rich Client Components Demo。
关于Demo的详细说明,比如下载、安装、源代码等等,请参考《 发布与运行ADF Faces Rich Client Components Demo 》。
访问http://jdevadf.oracle.com/adf-richclient-demo/faces/visualDesigns/activeCounter.jspx,可以看到运行效果:页面上的计数器会不断增长并自动刷新。
为了彻底搞清楚ActiveOutputText使用方法,我重新开发了一个应用,模仿Demo中的实现。
重点步骤说明:
1. ActiveOutputText组件页面代码
<af:activeOutputText value="#{counterBean.state}" id="aot1"
inlineStyle="color:brown;font-size:100px;font-weight:bold;text-align:center;"></af:activeOutputText>
2. 在adfc-config.xml中注册Managed Bean: CounterBean
3. 完整的Managed Bean代码
说明:
(1)CounterBean必须实现ActiveDataModel接口,这里是继承BaseActiveDataModel,后者实现了ActiveDataModel接口。
(2)使用ActiveModelContext注册该ActiveDataModel(即CounterBean),并把keyPath与"state"属性关联起来,这样当"state"属性发生变化时,Model层可以监听到,并通知视图层,视图层将会随之改变。
(3)使用Timer来定时启动任务UpdateTask,即执行UpdateTask类的run方法。
其中第1个参数2000表示第一次执行时延迟2秒钟, 第2个参数2000表示每2秒钟执行一次。
(4)UpdateTask类是一个多线程任务:增加计数器值,并且“引爆”数据改变事件。
(5)使用ActiveDataEventUtil创建事件,更新Model。
其中参数type表示改变事件的类型,可以设置为UPDATE, INSERT, DELETE, REFRESH,等等;
参数changeCount用来保证读取一致性;
参数key表示当前记录的key,适用于Model是CollectionModel类型的情况;
参数insertKey表示插入的位置,适用于Model是CollectionModel类型的情况;
参数names表示已改变的属性名称;
参数values表示已改变的属性值。
我们常用的组件是OutputText,它是用来显示Label的。那么,ActiveOutputText比OutputText特殊在哪里呢?
当内容发生改变时,ActiveOutputText组件可以帮助我们从后台“动态刷新”内容,用户无需做任何操作。
同样的功能如果使用OutputText组件来实现,就需要使用PPR的功能。
本实验取材于ADF Faces Rich Client Components Demo。
关于Demo的详细说明,比如下载、安装、源代码等等,请参考《 发布与运行ADF Faces Rich Client Components Demo 》。
访问http://jdevadf.oracle.com/adf-richclient-demo/faces/visualDesigns/activeCounter.jspx,可以看到运行效果:页面上的计数器会不断增长并自动刷新。
为了彻底搞清楚ActiveOutputText使用方法,我重新开发了一个应用,模仿Demo中的实现。
重点步骤说明:
1. ActiveOutputText组件页面代码
<af:activeOutputText value="#{counterBean.state}" id="aot1"
inlineStyle="color:brown;font-size:100px;font-weight:bold;text-align:center;"></af:activeOutputText>
2. 在adfc-config.xml中注册Managed Bean: CounterBean
<?xml version="1.0" encoding="UTF-8" ?> <adfc-config xmlns="http://xmlns.oracle.com/adf/controller" version="1.2"> <managed-bean id="__1"> <managed-bean-name>counterBean</managed-bean-name> <managed-bean-class>view.CounterBean</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> </adfc-config>
3. 完整的Managed Bean代码
package view; import java.util.Collection; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.atomic.AtomicInteger; import oracle.adf.view.rich.activedata.ActiveModelContext; import oracle.adf.view.rich.activedata.BaseActiveDataModel; import oracle.adf.view.rich.event.ActiveDataEntry; import oracle.adf.view.rich.event.ActiveDataUpdateEvent; import oracle.adf.view.rich.activedata.ActiveDataEventUtil; //import oracle.adfinternal.view.faces.activedata.ActiveDataEventUtil; public class CounterBean extends BaseActiveDataModel { public String getState() { ActiveModelContext context = ActiveModelContext.getActiveModelContext(); Object[] keyPath = new String[0]; context.addActiveModelInfo(this, keyPath, "state"); timer.schedule(new UpdateTask(), 2000, 2000); return String.valueOf(counter); } // not needed as we do not need to connect to a (real) active data source... protected void startActiveData(Collection<Object> rowKeys, int startChangeCount) { } // not needed as we do not need to disconnect from a (real) active data source... protected void stopActiveData(Collection<Object> rowKeys) { } public int getCurrentChangeCount() { return counter.get(); } protected class UpdateTask extends TimerTask { public void run() { counter.incrementAndGet(); ActiveDataUpdateEvent event = ActiveDataEventUtil.buildActiveDataUpdateEvent(ActiveDataEntry.ChangeType.UPDATE, counter.get(), new String[0], null, new String[] { "state" }, new Object[] { counter.get() }); fireActiveDataUpdate(event); } } private static final Timer timer = new Timer(); private final AtomicInteger counter = new AtomicInteger(0); }
说明:
(1)CounterBean必须实现ActiveDataModel接口,这里是继承BaseActiveDataModel,后者实现了ActiveDataModel接口。
(2)使用ActiveModelContext注册该ActiveDataModel(即CounterBean),并把keyPath与"state"属性关联起来,这样当"state"属性发生变化时,Model层可以监听到,并通知视图层,视图层将会随之改变。
(3)使用Timer来定时启动任务UpdateTask,即执行UpdateTask类的run方法。
其中第1个参数2000表示第一次执行时延迟2秒钟, 第2个参数2000表示每2秒钟执行一次。
(4)UpdateTask类是一个多线程任务:增加计数器值,并且“引爆”数据改变事件。
(5)使用ActiveDataEventUtil创建事件,更新Model。
其中参数type表示改变事件的类型,可以设置为UPDATE, INSERT, DELETE, REFRESH,等等;
参数changeCount用来保证读取一致性;
参数key表示当前记录的key,适用于Model是CollectionModel类型的情况;
参数insertKey表示插入的位置,适用于Model是CollectionModel类型的情况;
参数names表示已改变的属性名称;
参数values表示已改变的属性值。
Project 下载:ADF_ActiveOutputText.7z
http://maping930883.blogspot.com/2012/07/adf171activeoutputtext.html