如何知道哪个组件触发了p:ajax请求

时间:2012-11-23 11:04:00

标签: ajax jsf primefaces

我有一个带有p:ajax和监听器的多个输入字段。它们都连接到同一个侦听器。我怎么知道哪个组件触发了监听器?

<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();
}

我有这个问题,因为我曾经使用a4j ajax,但我正在将项目移动到完全素数,而不再是富像素。 a4j的监听器有一个AjaxBehaviorEvent事件,我可以在那里做event.getComponent()。getId()

我如何使用素数ajax做同样的事情?

2 个答案:

答案 0 :(得分:2)

AjaxBehaviorEvent并非特定于RichFaces。它特定于JSF2本身。所以你可以继续在PrimeFaces中使用它。

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

作为替代方案,或者对于其他地方无法实现 的情况,您可以始终使用新的JSF2 UIComponent#getCurrentComponent()方法。

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

顺便说一下,同样的构造应该可以正常使用JSF2自己的<f:ajax>。我认为没有理由在这里使用<p:ajax>。但是,如果您实际使用的是PrimeFaces组件,例如<p:inputText>,那将是唯一的方法。


对具体问题

无关event="change"已经是默认值。你可以省略它。

答案 1 :(得分:-2)

在主要方面几乎相同:

 <p:ajax event="change" listener="#{businessPartner.primaryAddress.retrievePostalCodeCity}"  />


import javax.faces.event.AjaxBehaviorEvent;
.....
public void retrievePostalCodeCity(AjaxBehaviorEvent event) {
...
}

如果您想通过按钮组件 action / actionListener 标记进行访问,可以使用ActionEvent,并且在任何情况下都要确保设置 ajax = “真”

<p:commandLink actionListener="#{businessPartner.primaryAddress.retrievePostalCodeCity}" ajax="true" />

import javax.faces.event.ActionEvent;

....

public void retrievePostalCodeCity(ActionEvent event) {
    ...
    }