【问题标题】:Spring 3.1 How do you send all exception to one page?Spring 3.1 如何将所有异常发送到一页?
【发布时间】:2012-09-24 16:46:00
【问题描述】:

我有以下

web.xml

<error-page>
    <error-code>500</error-code>
    <location>/WEB-INF/views/errorPages/500.jsp</location>
</error-page>

<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/WEB-INF/views/errorPages/error.jsp</location>
</error-page>

<error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/WEB-INF/views/errorPages/500.jsp</location>
</error-page>

spring-context.xml

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
    <props>
        <prop key="java.lang.Exception">error</prop>
    </props>
    </property>
</bean>

<bean id="exceptionResolver"
    class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">

    <property name="exceptionMappings">
        <props>
            <prop key="com.company.server.exception.GenericException">GenericExceptionPage</prop>
            <prop key="java.lang.Exception">error</prop>
        </props>
    </property>

    <property name="defaultErrorView" value="defaulterror"></property>
</bean>

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean> 

GenericException.java

public class GenericException extends RuntimeException
{
  private static final long serialVersionUID = 1L;

  private String customMsg;
  public String message;

  public String getCustomMsg()
  {
    return customMsg;
  }

  public void setCustomMsg(String customMsg)
  {
    this.customMsg = customMsg;
  }

  public GenericException(String customMsg)
  {
    this.customMsg = customMsg;
  }

  @Override
  public String getMessage()
  {
    return message;
  }

}

myController.java

@Controller
@RequestMapping(value = "/admin/store")
public class AdminController
{


 //bunch of restful drivin requestMappings


}

问题: 如何获取所有内部服务器错误和异常以将异常/错误消息显示到单个页面?

【问题讨论】:

  • 你检查this了吗?

标签: java spring exception-handling error-handling


【解决方案1】:

如果你想处理来自所有你的控制器的异常,你真的应该扩展SimpleMappingExceptionResolver,或者,AbstractHandlerExceptionResolver并在容器中配置它,如here所述。

这将防止您(或其他在代码中工作的人)必须将所有控制器子类化,并在一个地方处理异常。

使用注解和超类会起作用,但注解似乎更针对每个控制器的使用。

此外,这两个建议仅适用于控制器方法抛出的异常。

如果您担心 .jsp 文件中的异常,那么 this post 应该提供补充解决方案。

以上都不是 web.xml 错误页面配置的替代品,因为它们的范围不同。对于讨论this post 似乎是一个很好的起点。

【讨论】:

    【解决方案2】:

    我会考虑在我的控制器中的方法上使用@ExceptionHandler 注释。当 Exception 冒泡通过控制器时,此注解标记了 Spring 将调用的方法。

    这样,当您的 @RequestMapping 方法之一抛出 Exception 时,该方法将被调用并可以返回您想要的任何错误消息。

    public class BaseController {
        @ExceptionHandler(Throwable.class)
        public String handleException(Throwable t) {
            return "redirect:/errorPages/500.jsp";
        }
    
        @ExceptionHandler(Exception.class)
        public String handleException(Throwable t) {
            return "redirect:/errorPages/error.jsp";
        }
    }
    
    @Controller
    @RequestMapping(value = "/admin/store")
    public class AdminController extends BaseController
    {
        @RequestMapping(...)
        //Some method
    
        @RequestMapping(...)
        //Another method
    }
    

    【讨论】:

    • 鉴于您想在整个应用程序中捕获所有异常;你需要为每个正在使用的控制器创建一个@ExceptionHandler 吗?
    • 你可以使用一个通用的基类来保存@ExceptionHandler注解的方法。
    • 我对 Spring 有点陌生。您能否提供一个通用示例或指出其他人这样做的示例。非常感谢 nicholas.hauschild
    • 我更新了我的例子来展示你是如何做到的。请注意,AdminController 扩展了 BaseControllerBaseController@ExceptionHandler 注释方法所在的位置。
    猜你喜欢
    • 2012-11-19
    • 2016-03-18
    • 2012-02-12
    • 2022-06-11
    • 2012-01-19
    • 2019-08-07
    • 2019-02-16
    • 2013-10-06
    • 2012-09-30
    相关资源
    最近更新 更多