2012-09-05 41 views
1

我有一个情况我有以下JSF页面执行页面加载一个命令按钮的动作侦听器执行自动点击如何在命令按钮的JSF使用脚本

 <?xml version='1.0' encoding='UTF-8'?> 
     <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1" 
     xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:af="http://xmlns.oracle.com/adf/faces/rich" 
     xmlns:trh="http://myfaces.apache.org/trinidad/html"> 
     <jsp:directive.page contentType="text/html;charset=UTF-8"/> 

     <f:view> 

     <af:document id="d1" partialTriggers="pt1"> 
     <af:resource type="javascript"> 
     function callCustomButton(){ 
     var button =AdfPage.PAGE.findComponentByAbsoluteId("cbCustomFields"); 
     ActionEvent.queue(button,true); 
     } 
     </af:resource> 



    <af:clientListener method="callCustomButton" type="load"/> 

     <af:form id="f1"> 
    <af:pageTemplate viewId="/ESOPM_Base_Template.jspx" id="pt1"> 



    <f:facet name="main_content"> 

    <af:decorativeBox id="db1" theme="medium"> 
     <f:facet name="top"/> 
     <f:facet name="center"> 
     <af:panelGroupLayout layout="scroll" id="pgl2"> 
     <trh:tableLayout id="tblUpload" cellPadding="3" cellSpacing="3" halign="center" inlineStyle="font-size:1.2em; font-weight:bold; font-family:helvetica,sans-serif;"> 
       <trh:rowLayout id="rl1"> 

        <trh:cellFormat id="cf1">    
         <af:panelGroupLayout layout="horizontal" id="pgl3"> 

        <af:commandButton text="Custom Fields"      

        actionListener="#{EditProperties.generateCustomAttributeFields}" 
        id="cbCustomFields" partialSubmit="true" visible="true"/> 
         </af:panelGroupLayout> 
        </trh:cellFormat> 
       </trh:rowLayout> 

        </trh:cellFormat> 
       </trh:rowLayout> 

     </trh:tableLayout> 
     </af:panelGroupLayout> 
     </f:facet> 
     </af:decorativeBox> 
     </f:facet> 
</af:pageTemplate> 
</af:form> 
</af:document> 
</f:view> 

</jsp:root> 

命令按钮是“自定义字段”并试图通过客户端侦听器在页面中使用的JavaScript调用它。 但是,当用户加载此页面时,按钮不会被点击并且其操作侦听器操作不会被执行。 请帮助。

回答

1

这是adf-stuff JSF 2吗?如果是这样你可以通过使用preRenderViewlistener在页面加载执行一些服务器端:

<f:view> 
    <f:metadata> 
     <f:event type="preRenderView" listener="#{bean.doSomething}"/> 
    </f:metadata> 
</f:view> 

更新

另一种方法是使你的支持bean请求范围,并使用@PostConstruct注释初始化数据:

@ManagedBean 
@RequestScoped 
public class Bean { 
    @PostConstruct 
    public void init(){ 

    } 
} 

如果您有机会切换到另一个JSF组件库,我会推荐Primefaces。它们为客户端脚本执行backing bean方法提供了一个非常简单的解决方案。请登录https://www.primefaces.org/showcase/ui/ajax/remoteCommand.xhtml查看<p:remoteCommand>

1

在f:view(managed bean)中创建afterMethod并监听RENDER_RESPONSE阶段。然后,通过调用JS调用到

String script = "callCustomButton();"; 
FacesContext fctx = FacesContext.getCurrentInstance(); 
//Trinidad class 
ExtendedRenderKitService erks = null; 
//Service also is a Trinidad class 
erks = Service.getRenderKitService(fctx, ExtendedRenderKitService.class); 
erks.addScript(fctx, script); 

确保你避免双重调用通过视图范围

弗兰克 附注:设置内存标志您是否曾尝试在服务器上对受管Bean(侦听器)中的操作事件排队调用该函数