在 EL 中嵌套变量
是否可以使用 FacesContext
或其他隐式对象(如 request
、session
等)在 EL 中嵌套变量调用(如下所示)?这当然是行不通的。我收到这个错误
错误解析:#{myBean.myMethod(#{FacesContext.getCurrentInstance().getViewRoot().getViewId() })}
对于此尝试
<ui:include src="#{myBean.myMethod(#{FacesContext.getCurrentInstance().getViewRoot().getViewId() })}">
Is it possible to nest variable calls like below in EL using FacesContext
or other implicit objects like request
, session
, etc.? This of course is not working. I get this error
Error Parsing: #{myBean.myMethod(#{FacesContext.getCurrentInstance().getViewRoot().getViewId() })}
for this attempt
<ui:include src="#{myBean.myMethod(#{FacesContext.getCurrentInstance().getViewRoot().getViewId() })}">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这确实是无效的 EL 语法。不允许嵌套
#{}
。只需将整个表达式放在同一个#{}
中即可。另外,#{FacesContext}
并不存在于 Facelets 的 EL 范围中,它是#{facesContext}
并且它已经是当前实例。此外,如果它已经是 getter 方法,则不一定需要用括号指定整个方法名称。因此,这应该可以实现
(请注意,这仍然需要支持 Servlet 3.0 / EL 2.2 的目标容器)
This is indeed invalid EL syntax. Nesting of
#{}
is disallowed. Just put the whole expression inside the same#{}
. Plus, the#{FacesContext}
doesn't exist in Facelets' EL scope, it's#{facesContext}
and it's already the current instance. Further, you don't necessarily need to specify the entire method name with parentheses if it are getter method already.So, this should do
(note that this still requires a target container with Servlet 3.0 / EL 2.2 support)
为了补充 BalusC 的答案,我想评论一下,作为一般规则,使您的 EL 表达式尽可能简单,并将所有逻辑(尤其是复杂的逻辑)放在支持 bean 的 Java 中。为什么不在 MyBean 中创建一个新的 Java 方法来执行您想要的操作并引用它呢?
EL 非常强大,但在我看来,它的功能正在诱惑您将业务逻辑放在表示层中。
To add to BalusC's answer, I would like to comment that as a general rule, make your EL expressions as simple as possible and put all the logic -- particularly complex logic, in the Java of the backing bean. Why not just create a new Java method in MyBean that does what you want and just refer to that?
EL is very powerful but looks to me like its capability is tempting you to put business logic in the presentation layer.