代码之家  ›  专栏  ›  技术社区  ›  Incognito

ADF如何从bean触发clientListener?

  •  1
  • Incognito  · 技术社区  · 7 年前

    这就是我要做的。

    为此,我有一个javascript函数,它可以很好地完成工作。 为了获得需要推送的值,我必须调用一个外部API,其中包含一些依赖于用户单击的参数。

    我还在inputfield上设置了一个clientListener(valueChange)来调用我的javascript函数。

    问题是,在用户调用调用API的my serverListener事件之后,当将值推送到inputfield内部时,不会调用clientListener。

    我让字段可见以进行测试,我可以自己输入值,它可以工作,但是当bean在字段中推送值时,valueChange事件不会触发。

    在bean检索到值后,如何获取要触发的事件,或者如何直接从bean调用javascript函数?

    下面是我使用的代码示例

    <af:resource type="javascript">
         function pushValue(evt){
              // push values to parent page
              window.external.main.setFieldValue("fieldName", evt.getSource().getValue());
         }
         function callServerAPI(){
             // call the serverListener
             AdfCustomEvent.queue(it2, "callAPI", {somevalues}, true);
         }
    </af:resource>
    <af:inputText label="My hidden field" 
                  id="it2" 
                  binding="#{pageFlowScope.MyAppBean.hiddenField}"
                  clientComponent="true">
                  <af:clientListener method="pushValue" type="valueChange"/>
                  <af:serverListener type="callAPI"
                                    method="#{pageFlowScope.MyAppBean.callAPI}"/>
    </af:inputText>
    

    private RichInputText hiddenField;
    public void callAPI(ClientEvent clientEvent) throws Exception {
    
        String fieldValue = clientEvent.getParameters().get("somevalues").toString();
    
        try {
    
            // call the API and get the value needed in the hidden field
            hiddenField.setValue(fetchValue(fieldValue));
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        // refresh field
        AdfFacesContext.getCurrentInstance().addPartialTarget(hiddenField);        
    
    }
    

    提前感谢您的帮助!

    1 回复  |  直到 7 年前
        1
  •  1
  •   Cedric    7 年前

    本文 https://cedricleruth.com/how-to-execute-client-javascript-in-an-adf-java-bean-action/ 详情如下:

    /*** In YOURJSF.jsf button, or other component that need to execute a javascript on action, add : ****/
    <af:commandButton text="ClickMe" id="cb1" actionListener="#{YOURSCOPE.YOURJAVABEAN.clickToExecuteJavascriptAction}"/>
    
     /*** In YOURJAVABEAN.java class add : ***/
     public void clickToExecuteJavascriptAction(ActionEvent actionEvent) {
      this.executeClientJavascript("console.log('You just clicked : " + actionEvent.getSource() + " ')");
      //Note: if you use a java string value in this function you should escape it to avoid breaking the javascript.
      //Like this : stringValue.replaceAll("[^\\p{L}\\p{Z}]", " ")
     }
    
    //You should put this function in a util java class if you want to use it in multiple bean
    public static void executeClientJavascript(String script) {
     FacesContext facesContext = FacesContext.getCurrentInstance();
     ExtendedRenderKitService service = Service.getRenderKitService(facesContext, ExtendedRenderKitService.class);
     service.addScript(facesContext, script);
    }
    
    推荐文章