6

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 Answer 1

18

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I think I've figured out that just using "exception" does the same thing. Thank you!
To find occurrences you can use this RegEx: (\$\{)(.*?)(?<![(?:"|'))\.(abstract|assert|boolean|break|byte|case|catch|char|class|const|continue|default|do|double|else|enum|extends|final|finally|float|for|goto|if|implements|import|instanceof|int|interface|long|native|new|package|private|protected|public|return|short|static|strictfp|super|switch|synchronized|this|throw|throws|transient|try|void|volatile|while)\b(?!(?:"|')])(.*?)(\})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.