是否可以在动作属性中使用EL条件运算符? [英] Is it possible to use EL conditional operator in action attribute?

查看:115
本文介绍了是否可以在动作属性中使用EL条件运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

条件运算符可用于许多属性,例如已渲染",值"和其他属性.

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.E​​LException:无效的方法表达式

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 MethodExpressions in EL.

您基本上有2个选择:

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

  1. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆