当前位置 : 主页 > 编程语言 > java >

JSP表达式语言错误行为?

来源:互联网 收集:自由互联 发布时间:2021-06-25
我在 Linux上运行Tomcat 6.0.18. 我有一个使用这样的bean的JSP: jsp:useBean id="helper" type="com.example.SomeType" scope="request"/ 该页面使用表达式语言引用helper的属性,如下所示: !-- This works properly, b
我在 Linux上运行Tomcat 6.0.18.

我有一个使用这样的bean的JSP:

<jsp:useBean id="helper"
             type="com.example.SomeType"
             scope="request"/>

该页面使用表达式语言引用helper的属性,如下所示:

<!-- This works properly, but could fail silently if the bean name is incorrect. -->
<div><p>Here's some stuff: ${helper.stuff}</div>

在一些重构中,我错过了名称助手的出现,我注意到如果名称助手写得不正确,不会引发错误.不在屏幕上,而不在我的日志文件中.在输出中没有为表达式语言片段生成任何内容:

<!-- Wrong name! "foo" should be "helper" but no error is observed (other than missing ouput)! -->
<div><p>Here's some stuff: ${foo.stuff}</div>

现在,如果我使用以下具有帮助程序名称错误的JSP语法,则会引发错误(我的自定义错误页面显示,我在日志文件中看到异常):

<!-- Wrong name, but an error is raised. -->
<div><p>Here's some stuff: <jsp:getProperty name="foo" property="stuff"/></div>

在这种情况下,日志记录此条目:

SEVERE: requestURI: /some.jsp servletName: jsp statusCode: 500
org.apache.jasper.JasperException: Attempted a bean operation on a null object.

为了完整性,当bean名称正确时,jsp:getProperty语法正常工作:

<!-- Works properly, protects me from an incorrect name, but is more verbose than EL. -->
<div><p>Here's some stuff: <jsp:getProperty name="helper" property="stuff"/></div>

当我写${foo.stuff}时,为什么我没有看到错误?在这种情况下是否有一些控制错误报告的配置选项?

此行为将在 Expression Language Specification Version 2.1的第1.6节中介绍.

To evaluate expr-a[expr-b]:

If value-a is null:

  • If expr-a[expr-b] is the last property being resolved:
    • If the expression is a value expression and
      ValueExpression.getValue(context) was
      called to initiate this expression
      evaluation, return null.
    • Otherwise, throw PropertyNotFoundException. trying to
      de-reference null for an lvalue
  • Otherwise, return null.

(EL统一.和[]运算符)

网友评论