web-dev-qa-db-ja.com

EL条件演算子をアクション属性で使用することは可能ですか?

条件演算子は、「レンダリングされた」「値」などの多くの属性で機能します。

しかし、それは実際には機能しませんか?それとも私はそれを間違っていますか?

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

エラー:javax.el.E​​LException:有効なメソッド式ではありません

(primefaces ajaxアクション属性を使用してそれを実現しました)

26
djmj

これはサポートされていません。 action属性はMethodExpressionであると想定されていますが、条件演算子はそれをValueExpression構文にします。 ELのMethodExpressionsでこれがサポートされることはないと思います。

基本的に2つのオプションがあります。

  1. ジョブを委任する単一のアクションメソッドを作成します。

    _<h:commandButton ... action="#{bean.method}" />
    _

    _public String method() {
        return condition ? methodTrue() : methodFalse();
    }
    _

    必要に応じて、#{bean.method(condition)}によってメソッド引数として渡します。

  2. または、2つのボタンを条件付きでレンダリングします。

    _<h:commandButton ... action="#{bean.methodTrue}" rendered="#{bean.condition}" />
    <h:commandButton ... action="#{bean.methodFalse}" rendered="#{not bean.conditon}" />
    _
49
BalusC