注释掉的 Facelets 代码仍然调用 EL 表达式,如 #{bean.action()} 并导致 #{bean.action} 上的 javax.el.PropertyNotFoundException [英] Outcommented Facelets code still invokes EL expressions like #{bean.action()} and causes javax.el.PropertyNotFoundException on #{bean.action}

查看:16
本文介绍了注释掉的 Facelets 代码仍然调用 EL 表达式,如 #{bean.action()} 并导致 #{bean.action} 上的 javax.el.PropertyNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've the following code snippet in my Facelet:

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

它工作正常,但是当我这样评论时,

It works fine, but when I outcomment it like this,

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

当我用括号改变方法表达式时,

When I change the method expression with parentheses as below,

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

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

Then doesn't throw the exception, but it's still invoked.

这是怎么引起的,我该如何解决?

How is this caused and how can I solve it?

推荐答案

仔细查看堆栈跟踪.这是相关部分:

Look closer at the stack trace. Here's the relevant part:

...
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 标记.

It's thus evaluating EL in a comment block (recognizable by CommentInstruction). A comment block is considered as template text. Facelets evaluates by default also EL #{} in template text. It's like as if you're writing <p>#{screenShotBean.takeScreenshot}</p> without any JSF tag.

您有多种选择:

  1. 完全删除评论块.

  1. Remove the comment block altogether.

在注释中使用 作为前缀来转义 EL 表达式,如

Escape EL expressions in the comment by prefixing it with as in

#{screenShotBean.takeScreenshot}

这样他们就不会被评估.

so that they won't be evaluated.

<ui:remove> 这样它就不会出现在组件树中(也不会出现在生成的 HTML 输出中).

Wrap the entire comment block in <ui:remove> so that it doesn't appear in the component tree (nor in the generated HTML output).

通过将以下上下文参数添加到 web.xml 来禁用 Facelets 对所有评论的解析:

Disable parsing of all comments by Facelets by adding the following context parameter to web.xml:

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

请注意,在生成的 HTML 输出中不会以这种方式结束任何评论.

Note that no one comment will end up in generated HTML output this way.

这篇关于注释掉的 Facelets 代码仍然调用 EL 表达式,如 #{bean.action()} 并导致 #{bean.action} 上的 javax.el.PropertyNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆