In my JSF page this works:
${requestScope.put('test', 'data')}
${requestScope.get('test')}
This causes exception:
${viewScope.put('test', 'data')}
${viewScope.get('test')}
Exception:
java.lang.NullPointerException
javax.el.BeanELResolver.invoke(BeanELResolver.java:159)
javax.el.CompositeELResolver.invoke(CompositeELResolver.java:84)
org.apache.el.parser.AstValue.getValue(AstValue.java:157)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
com.sun.faces.facelets.el.ELText$ELTextVariable.writeText(ELText.java:238)
com.sun.faces.facelets.el.ELText$ELTextComposite.writeText(ELText.java:154)
com.sun.faces.facelets.compiler.TextInstruction.write(TextInstruction.java:85)
com.sun.faces.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:82)
com.sun.faces.facelets.compiler.UILeaf.encodeAll(UILeaf.java:183)
javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:456)
com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:133)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
How do I use other than requestScope
on JSF page? I can of course have a view scoped bean but I thought I can use the scope variables directly. I have Tomcat 8.0.21 with Mojarra 2.2.10.
This is not the right way to put data in a certain scope from the Facelets template on.
You're supposed to use <c:set>
for this.
Putting data in request scope:
<c:set var="test" value="data" scope="request" />
..
#{test}
Putting data in view scope:
<c:set var="test" value="data" scope="view" />
..
#{test}
The scope
attribute also supports session
and application
.
Putting data in the Flash scope is done a bit differently:
<c:set target="#{flash}" property="test" value="data" />
...
#{flash.test}
来源:https://stackoverflow.com/questions/30029705/how-to-put-data-in-view-scope-via-el-in-jsf-page