Search code examples
jakarta-eespring-mvcspring-roojspx

Setting response status for uncaught exception handler in Roo


I have a Roo application with standard mvc setup.

If I throw exception from my controller I get nicely rendered stack trace and status code 200 on client side.

I found that it is handled by error page defined in web.xml as:

<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/uncaughtException</location>
</error-page>

uncaughtException.jspx is:

<div xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:fn="http://java.sun.com/jsp/jstl/functions" xmlns:util="urn:jsptagdir:/WEB-INF/tags/util" xmlns:spring="http://www.springframework.org/tags" xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
  <jsp:directive.page contentType="text/html;charset=UTF-8" />
  <jsp:output omit-xml-declaration="yes" />
  <spring:message var="title" code="error_uncaughtexception_title" htmlEscape="false" />
  <util:panel id="title" title="${title}">
    <h2>${fn:escapeXml(title)}</h2>
    <p>
      <spring:message code="error_uncaughtexception_problemdescription" />
    </p>
    <c:if test="${not empty exception}">
      <p>
        <h4>
          <spring:message code="exception_details" />
        </h4>
        <spring:message var="message" code="exception_message" htmlEscape="false" />
        <util:panel id="_message" title="${message}" openPane="false">
          <c:out value="${exception.localizedMessage}" />
        </util:panel>
        <spring:message var="stacktrace" code="exception_stacktrace" htmlEscape="false" />
        <util:panel id="_exception" title="${stacktrace}" openPane="false">
          <c:forEach items="${exception.stackTrace}" var="trace">
            <c:out value="${trace}" />
            <br />
          </c:forEach>
        </util:panel>
      </p>
    </c:if>
  </util:panel>
</div>

How do I change the responce code to something different than 200 ?

Is this possible to somehow set it in uncaughtException.jspx?


Solution

  • Solves this by setting defaultStatusCode on org.springframework.web.servlet.handler.SimpleMappingExceptionResolver in webmvc-config.xml.

    Example:

    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" p:defaultErrorView="uncaughtException">
        <property name="exceptionMappings">
            <props>
                <prop key=".DataAccessException">dataAccessFailure</prop>
                <prop key=".NoSuchRequestHandlingMethodException">resourceNotFound</prop>
                <prop key=".TypeMismatchException">resourceNotFound</prop>
                <prop key=".MissingServletRequestParameterException">resourceNotFound</prop>
            </props>
        </property>
        <property name="defaultStatusCode">
            <value>500</value>
        </property>
    </bean>