开发者

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

开发者 https://www.devze.com 2023-03-10 15:55 出处:网络
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:

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 r开发者_JAVA百科equired 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)?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号