Nesting variables in EL

送分小仙女□ 提交于 2020-01-28 11:23:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!