JSF中的多个ActionListener

JSF中的多个ActionListener,jsf,jsf-2,primefaces,Jsf,Jsf 2,Primefaces,在进一步处理之前,我想使用多操作侦听器设置两个支持bean的状态 第一路: <p:commandButton process="@this" > <f:attribute name="key" value="#{node.getIdTestGroup()}" /> <f:actionListener binding="#{testController.nodeListener}" /> <f:actionListener binding="#

在进一步处理之前,我想使用多操作侦听器设置两个支持bean的状态

第一路:

<p:commandButton process="@this" >
   <f:attribute name="key" value="#{node.getIdTestGroup()}" />
   <f:actionListener binding="#{testController.nodeListener}" />
<f:actionListener binding="#{testDeviceGroupController.prepareCreate}" />
</p:commandButton>
<p:commandButton process="@this" >
    <f:attribute name="key" value="#{node.getIdTestGroup()}" />
    <f:actionListener binding="#{testController.nodeListener(event)}" />
    <f:actionListener binding="#{testDeviceGroupController.prepareCreate(event)}" />
</p:commandButton>

它给出了一个例外:

警告:/testGroup/List.xhtml@26,88 binding=“#{testController.nodeListener()}”:未找到方法nodeListener javax.el.ELException:/testGroup/List.xhtml@26,88 binding=“#{testController.nodeListener()}”:未找到方法nodeListener

第二种方式:

<p:commandButton process="@this" >
   <f:attribute name="key" value="#{node.getIdTestGroup()}" />
   <f:actionListener binding="#{testController.nodeListener}" />
<f:actionListener binding="#{testDeviceGroupController.prepareCreate}" />
</p:commandButton>
<p:commandButton process="@this" >
    <f:attribute name="key" value="#{node.getIdTestGroup()}" />
    <f:actionListener binding="#{testController.nodeListener(event)}" />
    <f:actionListener binding="#{testDeviceGroupController.prepareCreate(event)}" />
</p:commandButton>

nodeListener和prepareCreate方法上的事件为null


如何更正?

绑定属性值需要指向实现
ActionListener
接口的对象,而不是方法

f:actionListener
bindig
属性的文档中:

计算为实现javax.faces.event.ActionListener的对象的值绑定表达式


我们讨论了一个类似的问题。

我看到您简化了传统的方法,即使用纯粹的直觉和随机联想猜测它是如何工作的,然后表现出惊讶:-)

f:actionListener
仅允许将整个对象添加为观察者,而不是任意方法。您可以使用
type
属性指定类名(它将由JSF实例化),也可以使用
binding
属性提供您自己创建的对象的实例(不是方法!)。对象必须实现
javax.faces.event.ActionListener

您的第二次尝试(
testDeviceGroupController.prepareCreate(event)
)在许多级别上都是错误的,但关键是调用这些方法不是为了处理您的操作,而是为了创建
Actionlistener
实例

您有两个选择:

  • 最明智的做法是:只需创建一个调用每个目标方法的方法。因为它们在不同的bean上,所以可以将一个注入另一个
  • 如果这对您不起作用,您可以创建一个创建侦听器对象的方法
像这样:

public ActionListener createActionListener() {
    return new ActionListener() {
        @Override
        public void processAction(ActionEvent event) throws AbortProcessingException {
            System.out.println("here I have both the event object, and access to the enclosing bean");
        }
    };
}
然后像这样使用它:

<h:commandButton>
    <f:actionListener binding="#{whateverBean.createActionListener()}"/>            
</h:commandButton>


为了澄清,为什么*工作?(我以前只需要一个bean时使用过它)它可以直接创建侦听器对象,然后调用whateverBean.actionListenerMethod?@smolarek999:它可以工作,因为它应该像这样工作,并且在文档中有说明!commandbutton的actionListenerMethod调用方法的EL;actionlisteners绑定获取ActionListener对象的EL。你不能仅仅因为名字相似就猜测两件事是一样的。真的,猜测是一个糟糕的主意。罗夫:传统的猜测方法是使用纯粹的直觉和随机联想,然后做出令人惊讶的行为: