Search code examples
javaajaxjsfjsf-2dom-events

Difference between bean method with AjaxBehaviorEvent and bean method without AjaxBehaviorEvent?


I have my XHTML like this

<h:form id="form">
    <h:panelGrid columns="3">
        <h:outputText value="Keyup: " />
        <p:inputText id="counter">
            <p:ajax event="keyup" update="out"
                listener="#{counterBean.increment}" />
        </p:inputText>
        <h:outputText id="out" value="#{counterBean.count}" />
    </h:panelGrid>
</h:form>

Case I : ajax listener method with AjaxBehaviorEvent

public void increment(AjaxBehaviorEvent event) {
        count++;
    }

Case II : without AjaxBehaviorEvent

 public void increment() {
       count++;
}

In both the cases listener will be invoked and does the counter job to increase count on keyup. So, When exactly I need to use AjaxBehaviorEvent and when I don't need to use?


Solution

  • You can bind multiple ajax events to the same method and use getSource() of AjaxBehaviorEvent to know which component trigged the event.