How to know which component triggerd an p:ajax request

前端 未结 2 1772
有刺的猬
有刺的猬 2021-01-27 07:58

I have multiple input field with a p:ajax with a listener. They all connect to the same listener. How can I know what component triggerd the listener?



        
2条回答
  •  忘掉有多难
    2021-01-27 08:32

    The AjaxBehaviorEvent is not specific to RichFaces. It's specific to JSF2 itself. So you can just keep using it in PrimeFaces.

    public void retrievePostalCodeCity(AjaxBehaviorEvent event) {
        UIComponent component = event.getComponent();
        // ...
    }
    

    As an alternative, or for the case that it's really not possible elsewhere, you could always use the new JSF2 UIComponent#getCurrentComponent() method.

    public void retrievePostalCodeCity() {
        UIComponent component = UIComponent.getCurrentComponent(FacesContext.getCurrentInstance());
        // ...
    }
    

    By the way, the very same construct should work just fine with JSF2's own . I do not see any reason to use here. It would however be the only way if you were actually using a PrimeFaces component such as .


    Unrelated to the concrete problem, the event="change" is the default already. You can just omit it.

提交回复
热议问题