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

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

问题描述

如何能在我JSF 2.0调用带参数/变量/参数直接法或方法在EL?

例如,获取列表大小EL:

 < H:的outputText值=#{bean.list.size()}/>

或调用的操作方法与参数:

 < H:的commandButton值=编辑行动=#{bean.edit(项目)}/>

这似乎并不在我的环境中工作。它似乎并不喜欢括号。


  

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



解决方案

在标准EL来前EL 2.2从Java EE 6 你不能直接调用像结果的方法
#{bean.method()} ,也没有调用带有参数的方法,如#{bean.method(ARG1,ARG2)

如果升级到2.2的EL / 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是把<一的问题href=\"https://maven.atlassian.com/content/repositories/jboss-releases/org/jboss/seam/jboss-el/2.0.0.GA/jboss-el-2.0.0.GA.jar\"><$c$c>jboss-el.jar / WEB-INF / lib目录并添加以下到的web.xml ,假设你使用Mojarra

 &LT;的context-param&GT;
    &LT;参数-名称&gt; com.sun.faces.ex pressionFactory&LT; /参数 - 名称&gt;
    &LT;参数值&GT; org.jboss.el.E​​x pressionFactoryImpl&LT; /参数值&GT;
&LT; /的context-param&GT;

或者,当你使用MyFaces的:

 &LT;的context-param&GT;
    &LT;参数-名称&gt; org.apache.myfaces.EX preSSION_FACTORY&LT; /参数 - 名称&gt;
    &LT;参数值&GT; org.jboss.el.E​​x pressionFactoryImpl&LT; /参数值&GT;
&LT; /的context-param&GT;

一个替代的你的具体情况的是使用JSTL的<一个href=\"http://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/fn/length.fn.html\"><$c$c>fn:length:

 &LT; H:的outputText值=#{FN:长度(bean.list)}/&GT;

另一种方法是一个getter添加到它返回豆列表#尺寸()或在某些特定的情况下,<一个href=\"http://stackoverflow.com/questions/7079978/how-to-create-a-custom-el-function/7080174#7080174\">custom EL功能。


请注意这样的调用与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 / 2.2 EL向前兼容,所以它也有可能使用JSF 1.x中这个功能的话,还使用JBoss EL上的Servlet 2.5 / 2.1 EL

How can I in JSF 2.0 invoke direct methods or methods with arguments / variables / parameters in 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.ELException: Error Parsing: #{bean.list.size()}
com.sun.el.parser.ParseException: Encountered "("

解决方案

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).

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>

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>

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

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

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


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天全站免登陆