【问题标题】:Is it possible to use EL conditional operator in action attribute?是否可以在动作属性中使用 EL 条件运算符?
【发布时间】:2012-05-06 19:53:10
【问题描述】:

条件运算符适用于许多属性,如“渲染”、“值”等。

但它在行动中不起作用?还是我做错了?

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

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

(我是使用 primefaces ajax 动作属性实现的)

【问题讨论】:

标签: jsf action el conditional-operator


【解决方案1】:

不支持。 action 属性应该是 MethodExpression,但条件运算符使它成为 ValueExpression 语法。我认为 EL 中的 MethodExpressions 永远不会支持此功能。

你基本上有两个选择:

  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}" />
    

【讨论】:

  • @Alex:最适合您的模型的方式。然而,第二种方式有一个额外的要求,即必须在请求中保留条件,最好通过将 bean 放在视图范围内来实现,否则你会偶然发现stackoverflow.com/questions/2118656/…中描述为 #5 的问题>
  • @BalusC 视图范围正好满足我的要求,而且效果很好。再次感谢你。进一步的时间:亚历山大很好(不知道,但我讨厌亚历克斯)。 :)
  • 同样适用于 outputLink 吗?
  • @MichaelMiner:输出链接首先没有任何方法表达式属性。
  • 第二个选项是最好的!
猜你喜欢
  • 2012-08-30
  • 1970-01-01
  • 2011-02-21
  • 2017-02-01
  • 2021-10-10
  • 2011-10-05
  • 2018-01-04
  • 2015-05-05
  • 1970-01-01
相关资源
最近更新 更多