Outcommented Facelets代码仍调用#{bean.action()}之类的EL表达式,并在#{bean.action}上导致javax.el.PropertyNotFoundException

时间:2013-02-04 17:56:25

标签: jsf comments el facelets propertynotfoundexception

我的Facelet中有以下代码片段:

<h:commandLink id="cmdbtn">
    <f:ajax event="click" execute="@form"
            listener="#{screenShotBean.takeScreenshot}" />
</h:commandLink>

它工作正常,但当我像这样取消它时,

<!--        <h:commandLink id="cmdbtn"> -->
<!--            <f:ajax event="click" execute="@form" -->
<!--                    listener="#{screenShotBean.takeScreenshot}" /> -->
<!--        </h:commandLink> -->

然后抛出以下异常:

javax.el.PropertyNotFoundException: Property 'takeScreenshot' not found on type monstage.test.com.ScreenShotBean
    at javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:237)
    at javax.el.BeanELResolver$BeanProperties.access$400(BeanELResolver.java:214)
    at javax.el.BeanELResolver.property(BeanELResolver.java:325)
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:85)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:169)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
    at com.sun.faces.facelets.el.ELText$ELTextVariable.toString(ELText.java:217)
    at com.sun.faces.facelets.el.ELText$ELTextComposite.toString(ELText.java:157)
    at com.sun.faces.facelets.compiler.CommentInstruction.write(CommentInstruction.java:77)
    at com.sun.faces.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:82)
    at com.sun.faces.facelets.compiler.UILeaf.encodeAll(UILeaf.java:183)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:424)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:125)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)

当我用下面的圆括号更改方法表达式时,

<!--        <h:commandLink id="cmdbtn"> -->
<!--            <f:ajax event="click" execute="@form" -->
<!--                    listener="#{screenShotBean.takeScreenshot()}" /> -->
<!--        </h:commandLink> -->

然后不抛出异常,但它仍然被调用。

这是如何引起的?如何解决?

3 个答案:

答案 0 :(得分:16)

仔细观察堆栈跟踪。这是相关部分:

...
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
com.sun.faces.facelets.el.ELText$ELTextVariable.toString(ELText.java:217)
com.sun.faces.facelets.el.ELText$ELTextComposite.toString(ELText.java:157)
com.sun.faces.facelets.compiler.CommentInstruction.write(CommentInstruction.java:77)
...

因此,它在评论块中评估EL(可由CommentInstruction识别)。注释块被视为模板文本。 Facelets默认情况下还会在模板文本中评估EL #{}。就像你在没有任何JSF标签的情况下编写<p>#{screenShotBean.takeScreenshot}</p>一样。

您有几种选择:

  1. 完全删除评论栏。

  2. 通过在

    中添加\作为前缀,在评论中转义EL表达式
    \#{screenShotBean.takeScreenshot}
    

    这样他们就不会被评估。

  3. 将整个注释块包裹在<ui:remove>中,使其不会出现在组件树中(也不会出现在生成的HTML输出中)。

  4. 通过向web.xml添加以下上下文参数,禁止通过Facelets解析所有注释:

    <context-param>
        <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
        <param-value>true</param-value>
    </context-param>
    

    请注意,没有任何评论会以这种方式结束生成的HTML输出。

答案 1 :(得分:2)

options BalusC already provided之外,您还可以将属性rendered="false"添加到commandLink

如果您希望能够快速切换多个组件,则可能需要考虑在bean中创建调试属性或使用项目阶段:

rendered="#{facesContext.application.projectStage == 'Development'}"

答案 2 :(得分:1)

您还可以使用跳过web.xml中的注释的上下文参数。

这是参数:

javax.faces.FACELETS_SKIP_COMMENTS