Access exception.class.name in spring:message tag when using SimpleMappingExceptionResolver

让人想犯罪 __ 提交于 2019-12-07 03:13:06

问题


In several previous projects (all pre-Spring 3.0), I had a single error handling jsp file (usually "message.jsp") that had a line similar to the following:

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

This allowed me to map exceptions to this page and resolve certain localized error messages based on the exception type by defining a derivative of the 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>

This worked flawlessly until I tried upgrading to Spring 3 and Tomcat 7. Now, when I use this code, I get the following error:

"${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

Any idea what's changed or how to access the class name of the exception (part of the model returned by Spring to the mapped error page)?


回答1:


The EL implementation in Tomcat 7 has indeed been changed to disallow Java keyword literals such as class, new, static etcetera as EL properties.

The only solution as far is to access them using the brace notation instead:

${exception['class'].name}

See also Tomcat issue 50147.



来源:https://stackoverflow.com/questions/6258025/access-exception-class-name-in-springmessage-tag-when-using-simplemappingexcept

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!