Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 436
pconrey Avatar asked Jun 06 '11 21:06

pconrey


People also ask

How do you handle an exception in spring boot using annotations?

The @ExceptionHandler is an annotation used to handle the specific exceptions and sending the custom responses to the client. Define a class that extends the RuntimeException class. You can define the @ExceptionHandler method to handle the exceptions as shown.

Which exception class is related to all the exceptions that are thrown in Spring applications?

all the exceptions thrown by the spring jdbc framework are subclasses of dataaccessexception which is a type of runtimeexception, so you need not handle it explicitly.

What does SimpleMappingExceptionResolver allow you to do?

Class SimpleMappingExceptionResolver. HandlerExceptionResolver implementation that allows for mapping exception class names to view names, either for a set of given handlers or for all handlers in the DispatcherServlet.

How do you handle exceptions in a Spring web application?

You can add extra ( @ExceptionHandler ) methods to any controller to specifically handle exceptions thrown by request handling ( @RequestMapping ) methods in the same controller. Such methods can: Handle exceptions without the @ResponseStatus annotation (typically predefined exceptions that you didn't write)


1 Answers

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.

like image 123
BalusC Avatar answered Oct 15 '22 12:10

BalusC