Reputation: 2003
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?
<h:inputText id="postalCode" size="20" value="# businessPartner.primaryAddress.postalCode}"
<p:ajax event="change" listener="#{businessPartner.primaryAddress.retrievePostalCodeCity}" >
</p:ajax>
</h:inputText>
<h:inputText id="city" size="60" value="# businessPartner.primaryAddress.city}"
<p:ajax event="change" listener="#{businessPartner.primaryAddress.retrievePostalCodeCity}" >
</p:ajax>
</h:inputText>
public void retrievePostalCodeCity() throws MWSException {
int country = address.getCountryId();
String postalCode = address.getPostalCode();
String city = address.getCity();
}
I have this problem because I used to use a4j ajax, but I'm moving the project to fully primefaces and no longer richfaces. The listener to a4j has an AjaxBehaviorEvent event and there I could do event.getComponent().getId()
How can I do the same with prime ajax?
Upvotes: 0
Views: 3252
Reputation: 2790
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) {
...
}
Upvotes: -2
Reputation: 1109625
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.
Upvotes: 2