DD.*_*DD. 25 methods jsf arguments el parameter-passing
我如何在JSF 2.0中使用EL中的参数/变量/参数调用直接方法或方法?
例如,在EL中获取列表大小:
<h:outputText value="#{bean.list.size()}" />
Run Code Online (Sandbox Code Playgroud)
或者使用参数调用action方法:
<h:commandButton value="edit" action="#{bean.edit(item)}" />
Run Code Online (Sandbox Code Playgroud)
这在我的环境中似乎不起作用.它似乎不喜欢括号.
javax.el.ELException:错误解析:#{bean.list.size()}
com.sun.el.parser.ParseException:遇到"("
Bal*_*usC 55
在Java EE 6中EL 2.2之前的标准EL中,您不能直接调用类似
#{bean.method()}
方法,也不能使用类似参数调用方法#{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
,假设你使用钻嘴鱼科:
<context-param>
<param-name>com.sun.faces.expressionFactory</param-name>
<param-value>org.jboss.el.ExpressionFactoryImpl</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)
或者,当您使用MyFaces时:
<context-param>
<param-name>org.apache.myfaces.EXPRESSION_FACTORY</param-name>
<param-value>org.jboss.el.ExpressionFactoryImpl</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)
您的特定情况的替代方案是使用JSTL fn:length
:
<h:outputText value="#{fn:length(bean.list)}" />
Run Code Online (Sandbox Code Playgroud)
另一种方法是向bean添加一个getter,它返回List#size()
一些自定义EL函数或在某些特定情况下返回自定义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.
归档时间: |
|
查看次数: |
27276 次 |
最近记录: |