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

查看:99
本文介绍了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()}":找不到方法nodeListener javax.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-it-works-using-bare-intuition-and-random-associations-then-的传统方法意外的行为:-)

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天全站免登陆