【问题标题】:Setting response status for uncaught exception handler in Roo在 Roo 中为未捕获的异常处理程序设置响应状态
【发布时间】:2013-11-15 12:57:59
【问题描述】:

我有一个带有标准 mvc 设置的 Roo 应用程序。

如果我从控制器抛出异常,我会在客户端得到很好的堆栈跟踪和状态码 200。

我发现它是由web.xml中定义的错误页面处理的:

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

uncaughtException.jspx 是:

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

如何将响应代码更改为不同于 200 的内容?

这有可能以某种方式在uncaughtException.jspx 中设置吗?

【问题讨论】:

    标签: jakarta-ee spring-mvc spring-roo jspx


    【解决方案1】:

    通过在webmvc-config.xml 中将defaultStatusCode 设置为org.springframework.web.servlet.handler.SimpleMappingExceptionResolver 来解决此问题。

    例子:

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

    【讨论】:

    • 这个错误有什么提示吗? The prefix "p" for attribute "p:defaultErrorView" associated with an element type "beans:bean" is not bound.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-02
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    相关资源
    最近更新 更多