How to programmatically add an AjaxBehavior to a UIComponent with primefaces

后端 未结 1 1096
后悔当初
后悔当初 2020-12-19 07:56

Since i\'ve asked my last question (which still unanswered) i continued searching for a solution and lastly i found this topic which i think can help achieve what i want.

相关标签:
1条回答
  • 2020-12-19 08:08

    Do something like this

    Panel tp = new Panel();
    FacesContext fc = FacesContext.getCurrentInstance();
    ExpressionFactory ef = fc.getApplication().getExpressionFactory();
    
    MethodExpression me = ef.createMethodExpression(fc.getELContext(), "#{myView.closeIt}", null, new Class<?>[]{BehaviorEvent.class});
    AjaxBehavior ajaxBehavior = (AjaxBehavior) fc.getApplication().createBehavior(AjaxBehavior.BEHAVIOR_ID);
    ajaxBehavior.setProcess("@this");
    ajaxBehavior.addAjaxBehaviorListener(new AjaxBehaviorListenerImpl(me, me));
    tp.addClientBehavior("close", ajaxBehavior);
    
    component.getChildren().add(tp);
    

    myView.closeIt()

    public void closeIt(CloseEvent ce){
        Panel p = (Panel) ce.getComponent();
        System.out.println("Do what ever you want");
    }
    
    0 讨论(0)
提交回复
热议问题