问题描述
是否可以使用 FacesContext
或其他隐式对象(如 request
、session
等)在 EL 中嵌套如下所示的变量调用?这当然行不通.我收到此错误
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
错误解析:#{myBean.myMethod(#{FacesContext.getCurrentInstance().getViewRoot().getViewId() })}
为了这次尝试
<ui:include src="#{myBean.myMethod(#{FacesContext.getCurrentInstance().getViewRoot().getViewId() })}">
推荐答案
这确实是无效的 EL 语法.不允许嵌套 #{}
.只需将整个表达式放在同一个 #{}
中.另外,#{FacesContext}
在 Facelets 的 EL 范围中不存在,它是 #{facesContext}
并且它已经是当前实例.此外,如果它已经是 getter 方法,则不一定需要用括号指定整个方法名称.
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.
所以,应该这样做
<ui:include src="#{myBean.myMethod(facesContext.viewRoot.viewId)}">
(注意,这仍然需要一个支持 Servlet 3.0/EL 2.2 的目标容器)
这篇关于在 EL 中嵌套变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!