【问题标题】:Execute two methods in action in JSF在 JSF 中执行两个方法
【发布时间】:2011-12-16 07:03:09
【问题描述】:

<h:commandButton>的动作中是否可以执行两个方法?

例如,

<h:commandButton action="#{bean.methodOne();bean.methodTwo();}" />

【问题讨论】:

    标签: jsf jsf-2


    【解决方案1】:

    你可以像这样使用f:actionListener

      <h:commandButton action="#{bean.methodOne()}">
        <f:actionListener binding="#{bean.methodTwo()}" />
      </h:commandButton>
    

    您可以根据需要添加任意数量的f:actionListener 元素。

    【讨论】:

    【解决方案2】:

    在你的bean中添加一个methodThree:

    public Object methodThree() {
        methodOne();
        methodTwo();
        return someThing;
    }
    

    并从 JSF 页面调用此方法。

    【讨论】:

    • 我知道我可以做到,但我不能调用两个方法?
    • 来自h:commandButton action 属性的 Mojarra javadoc:MethodExpression 表示用户激活此组件时要调用的应用程序操作。表达式必须评估为不带参数的公共方法,并返回一个对象(调用其 toString() 以得出逻辑结果),该对象被传递给此应用程序的 NavigationHandler。
    • 谢谢马特。我编辑了答案以反映必须返回对象的事实。
    • 目前也允许使用void
    【解决方案3】:

    接受的答案对我来说已经很接近了,但是分号引发了解析异常。以下代码有效:

    <h:commandButton>
        <f:actionListener binding="#{bean.methodTwo()}" />
    </h:commandButton>
    

    【讨论】:

    • 这里只调用一种方法!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-05
    相关资源
    最近更新 更多