如何在 JSF 中调用带参数的方法
我有一个 JSF 页面,显示文件夹的内容(实际上是保管箱的帐户内容)。
我正在使用 dataTable 来呈现 ListArray 对象的内容:
<h:dataTable style="text-align: left" width="600" var="dContent" value="#{backedBean.contents}">
<h:column>
<f:facet name="header">
<f:verbatim>NAME</f:verbatim>
</f:facet>
<h:commandButton value="#{dContent.fileName}" action="#{backedBean.updateContents(dContent)}"/>
</h:column>
<h:column>
<f:facet name="header">
<f:verbatim>SIZE</f:verbatim>
</f:facet>
<h:outputText value="#{dContent.size}"/>
</h:column>
</h:dataTable>
但是当我运行此页面时,出现以下错误:
/browse.xhtml @34,110 action="#{backedBean.updateContents(dContent)}" 错误解析:#{backedBean.updateContents(dContent)}
...
...
引起:org.apache.el.parser.ParseException:遇到“ "(" "( "" 位于第 1 行第 28 列。期待以下之一:
“}”...
“。” ...
“[”...
“>” ...
“gt”...
“<” ...
“它”...
“>=”...
“ge” ...
...
...
有趣的是 Netbeans 能够自动完成方法名称,所以我认为我的后端 bean 没问题。仅当我调用带参数的方法时才会出现此问题。
有什么想法吗?
非常感谢
I' ve a JSF page that shows the content of a folder (really it's a dropbox's account content).
I'm using a dataTable to render the content of a ListArray object:
<h:dataTable style="text-align: left" width="600" var="dContent" value="#{backedBean.contents}">
<h:column>
<f:facet name="header">
<f:verbatim>NAME</f:verbatim>
</f:facet>
<h:commandButton value="#{dContent.fileName}" action="#{backedBean.updateContents(dContent)}"/>
</h:column>
<h:column>
<f:facet name="header">
<f:verbatim>SIZE</f:verbatim>
</f:facet>
<h:outputText value="#{dContent.size}"/>
</h:column>
</h:dataTable>
But when I run this page I obtain the following error:
/browse.xhtml @34,110 action="#{backedBean.updateContents(dContent)}"
Error Parsing: #{backedBean.updateContents(dContent)}
...
...
Caused by: org.apache.el.parser.ParseException: Encountered "
"(" "( "" at line 1, column 28. Was expecting one of:
"}" ...
"." ...
"[" ...
">" ...
"gt" ...
"<" ...
"lt" ...
">=" ...
"ge" ...
...
...
The funny thing is that Netbeans is able to autocomplete the method name so I image that my backend bean is ok. The problem occurs only when I call a method with a parameter.
Any ideas?
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
传递方法参数是在 EL 2.2 中引入的。因此,只有当您在支持 Servlet 3.0 / EL 2.2 的容器(例如 Tomcat 7、Glassfish 3、JBoss AS 6 等)以及您的
web.xml
上运行时,这才有可能是根据 Servlet 3.0 规范声明的。如果不是,请检查 此答案用于获取数据表中当前行的替代方案,或这个答案关于用支持传递方法参数的实现替换 EL 实现,以便您也可以在 Servlet 2.5 / EL 2.1 容器上使用它。
Passing method arguments was introduced in EL 2.2. So this is only possible if you're running on a Servlet 3.0 / EL 2.2 capable container like Tomcat 7, Glassfish 3, JBoss AS 6, etc and your
web.xml
is been declared as per Servlet 3.0 specification.If you aren't, then check this answer for alternatives with regard to obtaining current row in datatables, or this answer with regard to replacing the EL implementation by one which supports passing method arguments so that you can use it on Servlet 2.5 / EL 2.1 containers as well.
Jboss Seam 也可以帮助获得该功能。
Seam 使用 JBoss EL,它提供了标准统一表达语言 (EL) 的扩展。 JBoss EL 提供了许多增强功能,可以提高 EL 表达式的表现力和功能。
示例:
使用单引号传递文字字符串:
或
动态值
限制:
JBoss EL 目前无法与 JSP 2.1 一起用作编译器拒绝带有参数 in 的表达式。因此,如果您想在 JSF 1.2 中使用此扩展,则需要使用 Facelets。该扩展可以与 JSP 2.0 一起正常工作。
Jboss Seam can also help to get the feature.
Seam uses JBoss EL which provides an extension to the standard Unified Expression Language (EL). JBoss EL provides a number of enhancements that increase the expressiveness and power of EL expressions.
Example:
pass literal strings using single quotes:
<h:commandLink action="#{printer.println('Hello world!')}" value="Hello"/>
or
for dynamic value
<h:commandButton action="#{hotelBooking.bookHotel(hotel)}" value="Book Hotel"/>
Limitation:
JBoss EL can't currently be used with JSP 2.1 as the compiler rejects expressions with parameters in. So, if you want to use this extension with JSF 1.2, you will need to use Facelets. The extension works correctly with JSP 2.0.
自 JSF 1.0 以来,实际上已经有一种“黑客”方法可以做到这一点。您只需在支持 bean 上创建一个返回 Map 的方法,然后可以使用 JSF EL 将您想要的任何对象传递给该方法,因为 JSF 认为您正在将键传递给映射。
同时,在您的支持 bean 方法中,您实际上返回一个“冒名顶替者”地图实例,该实例根本不是真正的地图,其
get()
方法委托给您想要调用的方法。在 .xhtml 或 .jsp 文件中,您可以使用方括号表示法来传递变量。扩展 HashMap 是使 imposter 映射易于定义的一种方法——足够简洁,可以通过这种方式使用匿名内部类。
这是一个 hack,但过去对我来说效果很好。
There has actually been a "hack" way of doing this since JSF 1.0. You just create a method on your backing bean that returns a Map, and you can use JSF EL to pass whatever object you want to that method, because JSF thinks that you are passing the key to the map.
Meanwhile, in your backing bean method you actually return an "imposter" map instance that is not really a map at all, whose
get()
method delegates to the method you wanted to call. In your .xhtml or .jsp file then you can use the square bracket notation to pass the variable.Extending
HashMap
is one way to make the imposter map easy to define -- succinct enough to use an anonymous inner class that way.This is a hack, but it has worked well for me in the past.