本文介绍了在spring中访问exception.class.name:使用SimpleMappingExceptionResolver时的消息标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这允许我将异常映射到此页面,并根据异常类型解析某些本地化的错误消息定义SimpleMappingExceptionResolver的派生:

 < bean id =exceptionMappingclass =mycode.ui.resolvers.MyExceptionResolver > 
< property name =exceptionMappings>
<道具>
< 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}包含无效的表达式:javax。 el.ELException:标识符[class]不是EL规范第1.19节要求的有效Java标识符

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

解决方案

Tomcat 7中的EL实现确实已被更改为禁止Java关键字文字,例如 class new static etcetera作为EL属性。



唯一的解决方案是使用大括号访问它们符号代替:

  $ {exception ['class']。name} 
/ pre>

另请参见。


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)?

解决方案

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.

这篇关于在spring中访问exception.class.name:使用SimpleMappingExceptionResolver时的消息标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 21:42