JSF 中的多个动作侦听器 [英] Multiple actionlisteners in JSF

查看:26
本文介绍了JSF 中的多个动作侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to use multiple action listener to set state of two backing beans before further processing

第一种方式:

<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()}":未找到方法 nodeListenerjavax.el.E​​LException:/testGroup/List.xhtml @26,88 binding="#{testController.nodeListener()}": 方法 nodeListener 未找到

WARNING: /testGroup/List.xhtml @26,88 binding="#{testController.nodeListener()}": Method nodeListener not found javax.el.ELException: /testGroup/List.xhtml @26,88 binding="#{testController.nodeListener()}": Method nodeListener not found

第二种方式:

<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 方法上的事件为空

Event is null on the nodeListener and prepareCreate methods

如何正确操作?

推荐答案

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

I see you facilitate the traditional approach of guess-how-it-works-using-bare-intuition-and-random-associations-then-act-surprised :-)

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

f:actionListener only lets you add a whole object as an observer, not an arbitrary method. You can either use type attribute to specify the class name (it will be instantiated by JSF) or binding attribute to give an instance of the object that you created by yourself (not a method!). The object must implement javax.faces.event.ActionListener.

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

Your second try (testDeviceGroupController.prepareCreate(event)) is wrong on many levels, but the crux is that the methods are called not to handle your action, but to create the Actionlistener instance.

您有几个选择:

  • 最明智的做法:创建一个方法来调用每个目标方法.由于它们位于不同的 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>

这篇关于JSF 中的多个动作侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆