问题
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() })}">
回答1:
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
<ui:include src="#{myBean.myMethod(facesContext.viewRoot.viewId)}">
(note that this still requires a target container with Servlet 3.0 / EL 2.2 support)
回答2:
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.
来源:https://stackoverflow.com/questions/7183887/nesting-variables-in-el