How to put data in view scope via EL in JSF page

瘦欲@ 提交于 2019-12-01 06:58:37

问题


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.


回答1:


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

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