gpt4 book ai didi

JSP 表达式语言错误行为?

转载 作者:行者123 更新时间:2023-11-28 21:47:51 25 4
gpt4 key购买 nike

我在 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>

在一些重构过程中,我错过了名称 helper 的出现,我注意到如果名称 helper 不会引发错误写错了。不在屏幕上,也不在我的日志文件中。输出中的表达式语言片段不会生成任何内容:

<!-- 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 语法且 helper 的名称不正确,则会出现错误(显示我的自定义错误页面并且我在日志文件中看到异常) :

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

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

<!-- 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 统一了 . 和 [] 运算符)

关于JSP 表达式语言错误行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/912135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com