ADF中使用JavaScript

来源:互联网 发布:淘宝选词助手在哪里 编辑:程序博客网 时间:2024/05/22 03:26

1.引用JS文件 

<af:document> <af:resource type="javascript" source="/customJsCode.js"/> … </af:document>

2.定义JS代码 

<af:document> <af:resource type="javascript">    function customJsFunction(){      …     } </af:resource> …</af:document>

3.客户端组件 

1)使用af:clientListener来监听组件的各种事件。 
    设置组件的clientComponent属性 为 true。 
2)获得组件 
在ADF中能过AdfPage.PAGE对象来管理页面,可以通过下面的方法来查找页面上的组件。 
AdfPage.PAGE.findComponent,它是通过组件的客户端ID去查找组件,但是这个ID是不稳定的,会变化,因此建议应该在运行时由server端组件读取该ID,动态地加到JavaScript中。 
AdfPage.PAGE.findComponentByAbsoluteId ,这是一个推荐的方法,使用绝对ID去查找。 
3)客户端事件 
AdfComponentEvent是所有ADF组件事件的父类,它的queue(Boolean isPartial)可以使定义在组件上的事件排到事件队列里。 
例如,如果用户想用JS去调用一个按钮上的事件,可以使用AdfActionEvent.queue(commandButton, true | false)。 
AdfBaseEvent 里定义了 cancel(), stopBubbling(),setPartial() 去改变事件的行为。 

调用event.cancle()去阻止server对事件的操作。 

4)传递参数 
<af:clientListener .../> <af:clientAttribute name="colname" value="#{tableRow.bindings.firstName.name}"/>

---------------------

function copyValueToSelectedRows(evt) { var txtField = evt.getSource(); txtField.getProperty('colname'); } 

4.从客户端用JS调用server端的JAVA代码,即使用ADF的AJAX 

首先,使用af:clientListener 给客户端的按钮或某个需要事件的组件注册一个client listener, 它允许JS函数去监听组件的选择,鼠标移动,点击,键盘等多个事件。 
<af:clientListener method="onDoubleClick" type="dblClick"/>

JS代码 

function onDoubleClick (event) {                         var docComponment = AdfPage.PAGE.findComponentByAbsoluteId("d2");          AdfCustomEvent.queue(docComponment,                                          " sayHello ",                                           {} ,                                          true);        }

其次,使用af:serverListener注册一个server listener,当客户端事件被触发时它将会被执行。server listener的代码被定义在managed bean方法里,这个方法参数是ClientEvent最后返回空。 

<af:serverListener type="sayHello" method="#{helloManagedBean.sayhello}"/>

helloManagedBean的方法 
public void sayHello(ClientEvent clientEvent){        DCIteratorBinding helloIterator = ADFUtils.findIterator("helloIterator");        Row row = helloIterator.getCurrentRow();        ....    }

这样,当客户端的某个组件如button的事件被触发时就会调用写在helloManagedBean中的sayHello方法。 


5.ADF还提供了在JAVA代码中执行JS的方法 

例如,在某个操作后,想让页面跳转 

    private void skip(String page){        ExtendedRenderKitService erks =Service.getRenderKitService(FacesContext.getCurrentInstance(),                                        ExtendedRenderKitService.class);        String script = "window.location.replace(\"" + page+ "\")";               erks.addScript(FacesContext.getCurrentInstance(), script);    }

参考资料
    [1] Using JavaScript in Oracle ADF and Oracle ADF Faces Application Development
    [2] JavaScript API Reference for Oracle ADF Faces

0 0
原创粉丝点击