是否可以在动作属性中使用EL条件运算符? [英] Is it possible to use EL conditional operator in action attribute?
问题描述
条件运算符可用于许多属性,例如已渲染",值"和其他属性.
The conditional operator works in many attributes like "rendered" "value" and others.
但是它实际上不起作用吗?还是我做错了?
But it does not work in action? Or am I doing it wrong?
<h:commandLink action="#{true ? bean.methodTrue() : bean.methodFalse()}"/>
错误:javax.el.ELException:无效的方法表达式
Error: javax.el.ELException: Not a Valid Method Expression
(我使用primefaces ajax动作属性意识到了这一点)
(I realized it using primefaces ajax action attribute)
推荐答案
不支持此功能. action
属性应该是MethodExpression
,但是条件运算符使它成为ValueExpression
语法.我认为EL中的MethodExpression
永远不会支持此功能.
This is not supported. The action
attribute is supposed to be a MethodExpression
, but the conditional operator makes it a ValueExpression
syntax. I don't think this will ever be supported for MethodExpression
s in EL.
您基本上有2个选择:
-
创建一个委派工作的单一操作方法.
Create a single action method which delegates the job.
<h:commandButton ... action="#{bean.method}" />
使用
public String method() {
return condition ? methodTrue() : methodFalse();
}
如有必要,将其作为方法参数由#{bean.method(condition)}
传递.
或者,有条件地渲染2个按钮.
Or, conditionally render 2 buttons.
<h:commandButton ... action="#{bean.methodTrue}" rendered="#{bean.condition}" />
<h:commandButton ... action="#{bean.methodFalse}" rendered="#{not bean.conditon}" />
这篇关于是否可以在动作属性中使用EL条件运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!