Outcommented Facelets的代码仍然调用EL表达式像#{bean.action()}

2019-07-19 05:34发布

我在我的facelet下面的代码片段:

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

它工作正常,但是当我outcomment像这样,

<!--        <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> -->

这时不会抛出异常,但它仍然被调用。

这是怎么造成的,我该怎么解决呢?

Answer 1:

看看堆栈跟踪近。 下面是相关的部分:

...
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 #{}在模板文本。 这就像,如果你正在写<p>#{screenShotBean.takeScreenshot}</p>无任何JSF标签。

你已经几个选项:

  1. 完全删除注释块。

  2. 用前缀逃避它在评论EL表达式\作为

     \#{screenShotBean.takeScreenshot} 

    所以,他们不会进行评估。

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

  4. 通过将下面的环境参数禁用由Facelets的所有评论解析web.xml

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

    请注意,没有人评论将在生成的HTML输出落得如此下场。



Answer 2:

除了BalusC已经提供的选项 ,你也可以在属性加rendered="false"commandLink

如果你有你想成为能够快速切换几个组件,你可能要考虑在一个bean创建调试属性或使用项目阶段:

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


Answer 3:

你也可以使用在web.xml跳过注释的上下文参数。

这是参数:

javax.faces.FACELETS_SKIP_COMMENTS 


文章来源: Outcommented Facelets code still invokes EL expressions like #{bean.action()} and causes javax.el.PropertyNotFoundException on #{bean.action}