JSF 中具有单个命令组件的多个动作侦听器 [英] Multiple action listeners with a single command component in JSF

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

问题描述

是否可以使用单个命令组件调用多个侦听器方法?例如,

Is it possible to invoke more than one listener method using a single command component? For example,

一个视图范围的bean:

A view scoped bean:

@ManagedBean
@ViewScoped
public final class ViewScopedBean implements Serializable
{
    @ManagedProperty(value = "#{sessionScopedBean}")
    private SessionScopedBean sessionScopedBean; //Getter/Setter.
    private static final long serialVersionUID = 1L;

    public ViewScopedBean() {}

    public void action()
    {
        //Do something.
        sessionScopedBean.action();
    }
}

会话范围的 bean:

A session scoped bean:

@ManagedBean
@SessionScoped
public final class SessionScopedBean implements Serializable
{
    private static final long serialVersionUID = 1L;

    public SessionScopedBean () {}

    public void action() {
        //Do something.
    }
}

如下所示的命令按钮,

<h:commandButton value="Action" actionListener="#{viewScopedBean.action}"/>

调用 ViewScopedBean 中的 action() 方法,后者又调用 SessionScopedBean 中的 action() 方法通过注入该 bean 的一个实例.

invokes the method action() in ViewScopedBean which in turn invokes the action() method in SessionScopedBean by injecting an instance of that bean.

是否有可能在 XHTML 上做同样的事情,从而消除为了调用方法而注入 bean 的需要?

Is it somehow possible do the same on XHTML so that a need to inject a bean just to invoke a method can be eliminated?

推荐答案

使用 :

<h:commandButton value="Action">
    <f:actionListener binding="#{viewScopedBean.action()}"/>
    <f:actionListener binding="#{sessionScopedBean.action()}"/>
</h:commandButton />

请注意 EL 中括号的重要性.在这个特定的例子中省略它们会抛出一个令人困惑的 javax.el.PropertyNotFoundException: Property 'action' not found on type com.example.ViewScopedBean,因为它默认被解释为一个值表达式.添加括号使其成为方法表达式.另见 为什么我可以绑定 <f:actionListener>如果 JSF 不支持任意方法?

Note the importance of the parentheses in EL. Omitting them would in this particular example otherwise throw a confusing javax.el.PropertyNotFoundException: Property 'action' not found on type com.example.ViewScopedBean, because it's by default interpreted as a value expression. Adding parentheses makes it a method expression. See also Why am I able to bind <f:actionListener> to an arbitrary method if it's not supported by JSF?

您甚至可以以通常的方式向组件添加 actionListener 和/或 action 方法,稍后会调用该方法.它必须独一无二的是 action 方法,它决定了处理的结果.

You could even add an actionListener and/or an action method to the component the usual way, which is invoked later on. What it has to be unique is the action method, which decides the outcome for the processing.

无论如何,请记住侦听器总是在动作之前执行并被视为它的热身".你最好在action方法中执行整个逻辑,即使你需要做bean注入.

Anyway, keep in mind the listeners are always executed before the action and considered a "warming-up" for it. Your best is to perform the whole logic in the action method, even if you need to do bean injections.

另见:

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

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