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 @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.
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.
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.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With