是否可以在action属性中使用EL条件运算符?

djm*_*jmj 25 jsf action el conditional-operator

条件运算符适用于许多属性,如"已渲染","值"等.

但它不起作用?或者我做错了吗?

<h:commandLink action="#{true ? bean.methodTrue() : bean.methodFalse()}"/>
Run Code Online (Sandbox Code Playgroud)

错误:javax.el.E​​LException:不是有效的方法表达式

(我用primefaces ajax action属性实现了它)

Bal*_*usC 50

这不受支持.该action属性应该是a MethodExpression,但条件运算符使其成为ValueExpression语法.我不认为MethodExpressionEL 会支持这种情况.

你基本上有两个选择:

  1. 创建一个委派作业的单一操作方法.

    <h:commandButton ... action="#{bean.method}" />
    
    Run Code Online (Sandbox Code Playgroud)

    public String method() {
        return condition ? methodTrue() : methodFalse();
    }
    
    Run Code Online (Sandbox Code Playgroud)

    如有必要,将其作为方法参数传递给#{bean.method(condition)}.

  2. 或者,有条件地渲染2个按钮.

    <h:commandButton ... action="#{bean.methodTrue}" rendered="#{bean.condition}" />
    <h:commandButton ... action="#{bean.methodFalse}" rendered="#{not bean.conditon}" />
    
    Run Code Online (Sandbox Code Playgroud)