jsf - JSF 中的多个 Action 监听器

标签 jsf jsf-2 primefaces

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

第一种方式:

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

它给出了一个异常(exception):

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

怎么做才对?

最佳答案

我看到你促进了猜测它是如何工作的传统方法,使用裸直觉和随机关联 - 然后 - 行动 - 惊讶 :-)
f:actionListener仅允许您将整个对象添加为观察者,而不是任意方法。您可以使用 type属性来指定类名(它将由 JSF 实例化)或 binding属性来提供您自己创建的对象的实例(不是方法!)。对象必须实现 javax.faces.event.ActionListener .

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

你有几个选择:

  • 最理智的:只需创建一个调用每个目标方法的方法。由于它们位于不同的 bean 上,您可以将一个注入(inject)另一个。
  • 如果这对您不起作用,您可以创建一个创建监听器对象的方法。

  • 像这样:
    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 - JSF 中的多个 Action 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16374012/

    相关文章:

    ajax - h :inputText? 上的条件键盘监听器

    ajax - 如何在 p :ajax event=change? 时禁用 selectOneMenu 上的所需标签

    jsf - 如何在其他定义内设置定义

    ajax - 在 JSF 2 h :inputText 中按 Enter 时在 bean 中调用监听器

    css - selectonemenu jsf 上的属性

    primefaces - PrimeFaces 从版本 x 迁移到更新版本

    java - 使用 Java EE Bean 保存表单数据列表并打印 JSF XHTML 数据表中的数据

    Javascript 未在 JSF + Primefaces 页面中执行?

    jsf - 如何更改 p :datatable dynamically 的延迟加载的 setRowCount

    ajax - Primefaces Ajax 调用 Javascript