How to know which component triggerd an p:ajax request

前端 未结 2 1728
有刺的猬
有刺的猬 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 <f:ajax>. I do not see any reason to use <p:ajax> here. It would however be the only way if you were actually using a PrimeFaces component such as <p:inputText>.


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

    0 讨论(0)
  • 2021-01-27 08:41

    It is almost same in primefaces:

     <p:ajax event="change" listener="#{businessPartner.primaryAddress.retrievePostalCodeCity}"  />
    
    
    import javax.faces.event.AjaxBehaviorEvent;
    .....
    public void retrievePostalCodeCity(AjaxBehaviorEvent event) {
    ...
    }
    

    If you want to access via button component action/actionListener tag you can use ActionEvent and for any case make sure you set ajax="true":

    <p:commandLink actionListener="#{businessPartner.primaryAddress.retrievePostalCodeCity}" ajax="true" />
    
    import javax.faces.event.ActionEvent;
    
    ....
    
    public void retrievePostalCodeCity(ActionEvent event) {
        ...
        }
    
    0 讨论(0)
提交回复
热议问题