知道哪个组件触发了

知道哪个组件触发了

本文介绍了如何知道哪个组件触发了p:ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我遇到了这个问题,因为我曾经使用过a4j ajax,但是我将项目移到了全素面,而不再是richfaces. A4J的侦听器有一个AjaxBehaviorEvent事件,在那里我可以做event.getComponent().getId()

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

我如何使用Prime Ajax做同样的事情?

How can I do the same with prime ajax?

推荐答案

AjaxBehaviorEvent 并非特定于RichFaces.它特定于JSF2本身.因此,您只需在PrimeFaces中继续使用它即可.

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

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

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

顺便说一句,完全相同的构造应该可以与JSF2自己的<f:ajax>一起很好地工作.我看不出在这里使用<p:ajax>的任何理由.但是,如果您实际使用的是诸如<p:inputText>之类的PrimeFaces组件,那将是唯一的方法.

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>.

无关与具体问题无关,event="change"已经是默认问题.您可以忽略它.

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

这篇关于如何知道哪个组件触发了p:ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:57