【问题标题】:Access exception.class.name in spring:message tag when using SimpleMappingExceptionResolver使用 SimpleMappingExceptionResolver 时在 spring:message 标签中访问 exception.class.name
【发布时间】:2011-09-09 14:58:55
【问题描述】:

在之前的几个项目中(都是 Spring 3.0 之前的项目),我有一个错误处理 jsp 文件(通常是“message.jsp”),其中包含类似于以下内容的行:

<spring:message code="exceptions.${exception.class.name}" text="${exception.message}"/>

这使我可以将异常映射到此页面,并通过定义 SimpleMappingExceptionResolver 的派生项来根据异常类型解决某些本地化错误消息:

<bean id="exceptionMapping" class="mycode.ui.resolvers.MyExceptionResolver">
  <property name="exceptionMappings">
    <props>
      <prop key="java.lang.Exception">message</prop>
      <prop key="javax.servlet.ServletException">message</prop>
      <prop key="MyCustomException">message</prop>
      <prop key="ApplicationAuthorizationException">login</prop>
      <prop key="NotLoggedInException">login</prop>
    </props>
  </property>
</bean>

在我尝试升级到 Spring 3 和 Tomcat 7 之前,这一直运行良好。现在,当我使用此代码时,出现以下错误:

"${exception.class.name}" contains invalid expression(s): javax.el.ELException: The identifier [class] is not a valid Java identifier as required by section 1.19 of the EL specification

知道发生了什么变化或如何访问异常的类名(Spring 返回到映射错误页面的模型的一部分)?

【问题讨论】:

    标签: spring servlets exception-handling jstl


    【解决方案1】:

    Tomcat 7 中的 EL 实现确实已更改为禁止将 Java 关键字文字(例如 classnewstatic 等)作为 EL 属性。

    目前唯一的解决方案是使用大括号符号来访问它们:

    ${exception['class'].name}
    

    另见Tomcat issue 50147

    【讨论】:

    • 我想我已经发现只使用“异常”可以做同样的事情。谢谢!
    • 要查找匹配项,您可以使用此正则表达式: (\$\{)(.*?)(?
    猜你喜欢
    • 2014-12-19
    • 2013-06-09
    • 2011-04-01
    • 2010-12-06
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多