在 EL 中调用直接方法或带参数/变量/参数的方法 [英] Invoke direct methods or methods with arguments / variables / parameters in EL

查看:35
本文介绍了在 EL 中调用直接方法或带参数/变量/参数的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 JSF 2.0 中调用直接方法或在 EL 中使用参数/变量/参数调用方法?

How can I in JSF 2.0 invoke direct methods or methods with arguments / variables / parameters in EL?

例如获取EL中的列表大小:

For example, getting the list size in EL:

<h:outputText value="#{bean.list.size()}" />

或者调用带有参数的操作方法:

Or invoking an action method with arguments:

<h:commandButton value="edit" action="#{bean.edit(item)}" />

这在我的环境中似乎不起作用.好像不喜欢括号.

This does not seem to work in my environment. It doesn't seem to like parentheses.

javax.el.E​​LException:错误解析:#{bean.list.size()}
com.sun.el.parser.ParseException: 遇到("

javax.el.ELException: Error Parsing: #{bean.list.size()}
com.sun.el.parser.ParseException: Encountered "("

推荐答案

来自 Java EE 6 的 EL 2.2 你不能直接调用像
这样的方法#{bean.method()} 也不使用像 #{bean.method(arg1, arg2) 这样的参数调用方法.

In standard EL prior to EL 2.2 from Java EE 6 you cannot directly invoke methods like
#{bean.method()} nor invoke methods with arguments like #{bean.method(arg1, arg2).

如果升级到符合 EL 2.2/Java EE 6 的容器(Tomcat 7、Glassfish 3、JBoss AS 6 等)不是一种选择,并且您当前使用的是 EL 2.1/Java EE 5(Tomcat 6、Glassfish 2、JBoss AS 4 等),那么最好的办法是安装 JBoss EL.JBoss EL 是一个符合 EL 2.1 的实现,它支持与 EL 2.2 相同的特性.安装 JBoss EL 是将 jboss-el.jar/WEB-INF/lib 并添加以下内容到 web.xml,假设您使用的是 Mojarra:

If upgrading to a EL 2.2 / Java EE 6 compliant container (Tomcat 7, Glassfish 3, JBoss AS 6, etc) is not an option and you're currently using EL 2.1 / Java EE 5 (Tomcat 6, Glassfish 2, JBoss AS 4, etc), then your best bet is to install JBoss EL. JBoss EL is an EL 2.1 compliant implementation which supports the same features as EL 2.2. Installing JBoss EL is a matter of putting the jboss-el.jar in /WEB-INF/lib and adding the following to the web.xml, assuming that you're using Mojarra:

<context-param>     
    <param-name>com.sun.faces.expressionFactory</param-name>
    <param-value>org.jboss.el.ExpressionFactoryImpl</param-value>   
</context-param>

或者,当您使用 MyFaces 时:

Or, when you're using MyFaces:

<context-param>     
    <param-name>org.apache.myfaces.EXPRESSION_FACTORY</param-name>
    <param-value>org.jboss.el.ExpressionFactoryImpl</param-value>   
</context-param>

您的特殊情况的替代方法是使用 JSTL 的 fn:length:

An alternative for your particular case is to use JSTL's fn:length:

<h:outputText value="#{fn:length(bean.list)}" />

另一种选择是向 bean 添加一个 getter,它返回 List#size() 或在某些特定情况下一个 自定义 EL 函数.

Another alternative is to add a getter to the bean which returns List#size() or in some specific cases a custom EL function.

因此请注意,在 EL 中调用带参数的方法不是 JSF 2.0 的特定功能.这是 EL 2.2 特有的功能.EL 2.2 是 Java EE 6 的一部分,而 JSF 2.0 也是其中的一部分.所以它看起来像一个 JSF 2.0 特定的特性,但它不是.JSF 2.0 向后兼容缺少此功能的 Servlet 2.5/EL 2.1.另一方面,JSF 1.x 向前兼容 Servlet 3.0/EL 2.2,因此也可以在 JSF 1.x 中使用此功能,也可以在 Servlet 2.5/EL 2.1 上使用 JBoss EL.

Please note thus that invoking methods with arguments in EL is not a JSF 2.0 specific feature. It's an EL 2.2 specific feature. EL 2.2 is part of Java EE 6, which JSF 2.0 is also part of. So it look like a JSF 2.0 specific feature, but it isn't. JSF 2.0 is backwards compatible with Servlet 2.5 / EL 2.1 which lacks this feature. On the other hand, JSF 1.x is forwards compatible with Servlet 3.0 / EL 2.2, so it would also be possible to use this feature in JSF 1.x then, also using JBoss EL on Servlet 2.5 / EL 2.1.

这篇关于在 EL 中调用直接方法或带参数/变量/参数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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