【问题标题】:Multiple actionlisteners in JSFJSF 中的多个动作监听器
【发布时间】:2013-04-28 17:57:49
【问题描述】:

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

第一种方式:

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

它给出了一个例外:

警告:/testGroup/List.xhtml @26,88 binding="#{testController.nodeListener()}":找不到方法 nodeListener javax.el.E​​LException: /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(event)}" />
    <f:actionListener binding="#{testDeviceGroupController.prepareCreate(event)}" />
</p:commandButton>

nodeListener 和 prepareCreate 方法上的事件为空

如何正确处理?

【问题讨论】:

标签: jsf jsf-2 primefaces


【解决方案1】:

我看到你促进了传统方法 guess-how-it-works-using-bare-intuition-and-random-associations-then-act-surprised :-)

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。你不能仅仅因为名字相似就猜测两件事是一样的。真的,猜测是个坏主意。
  • ROFL:猜测如何工作的传统方法-使用-裸直觉-和-随机-关联-然后-行动-惊奇
【解决方案2】:

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

来自f:actionListener的bindig属性的文档:

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

here 讨论了类似的问题。

【讨论】:

    猜你喜欢
    • 2011-01-17
    • 2012-08-26
    • 2021-07-02
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    相关资源
    最近更新 更多