1.spring MVC中定义了一个标准的异常处理类SimpleMappingExceptionResolver

该类实现了接口HandlerExceptionResolver

2.看下SimpleMappingExceptionResolver都包含那些东西吧

//默认的异常名字
public static final String DEFAULT_EXCEPTION_ATTRIBUTE = "exception";
//异常列表,包括什么异常要对应什么一场页面处理
private Properties exceptionMappings;
//排除的异常
private Class<?>[] excludedExceptions;
//默认异常处理页面
private String defaultErrorView;
//默认的状态 例如:500
private Integer defaultStatusCode;
//状态列表
private Map<String, Integer> statusCodes = new HashMap<String, Integer>();
//异常名字
private String exceptionAttribute = DEFAULT_EXCEPTION_ATTRIBUTE;

上面列出的属性,一般我们用到的Properties,defaultErrorView这两个。

下面看看在spring-mvc.xml中的通用配置吧:

<!-- 统一异常处理 -->
<bean id="handlerExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!-- 定义的默认错误处理页面 -->
<property name="defaultErrorView" value="error"></property>
<!-- 定义错误类型和对应的页面处理 列表 -->
<property name="exceptionMappings">
<props>
<!-- 举个例子 -->
<prop key="java.sql.SQLException">error/sql</prop>
</props>
</property>
</bean>

3.当然我们做完这些工作,还不能完全把所有的异常信息都处理好,有些像404的我们就无能为力了,解决的方法就是在web.xml中定义<error-page>来处理了

<error-page>
<error-code>404</error-code>
<location>/error/404.jsp</location>
</error-page>

 

大体上一个常规的统一异常处理就完工了!

spring MVC中的异常统一处理的更多相关文章

  1. spring MVC中定义异常页面

    如果我们在使用Spring MVC的过程中,想自定义异常页面的话,我们可以使用DispatcherServlet来指定异常页面,具体的做法很简单: 下面看我曾经的一个项目的spring配置文件: 1 ...

  2. Spring MVC中的HandlerMapping与HandlerAdapter

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  3. Spring MVC源码(四) ----- 统一异常处理原理解析

    SpringMVC除了对请求URL的路由处理特别方便外,还支持对异常的统一处理机制,可以对业务操作时抛出的异常,unchecked异常以及状态码的异常进行统一处理.SpringMVC既提供简单的配置类 ...

  4. Spring MVC 中的基于注解的 Controller【转】

    原文地址:http://my.oschina.net/abian/blog/128028 终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 H ...

  5. Spring MVC中基于注解的 Controller

         终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响 ...

  6. Spring MVC 中 @ModelAttribute 注解的妙用

    Spring MVC 中 @ModelAttribute 注解的妙用 Spring MVC 提供的这种基于注释的编程模型,极大的简化了 web 应用的开发.其中 @Controller 和 @Rest ...

  7. Spring Boot 中全局异常处理器

    Spring Boot 中全局异常处理器,就是把错误异常统一处理的方法.等价于Springmvc中的异常处理器. 步骤一:基于前面的springBoot入门小demo修改 步骤二:修改HelloCon ...

  8. Spring MVC中注解的简介

    参考网址:  https://blog.csdn.net/a67474506/article/details/46361195 @RequestMapping映射请求 SpringMVC 使用 @Re ...

  9. Spring MVC 中的基于注解的 Controller(转载)

           终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法 ...

随机推荐

  1. 文件模式设置用户ID/设置组ID/sticky bit_转

    S_ISUID  (04000)  set-user-ID (set process effective user ID on execve(2))S_ISGID  (02000)  set-grou ...

  2. docker Failed to get D-Bus connection 报错 docker run -e MYVAR1 --env MYVAR2=foo --env-file ./env.list ubuntu bash

    docker Failed to get D-Bus connection 报错 原创憬薇2016-01-15 11:25:26评论(10)40278人阅读   在centos7的容器里面出现了一个B ...

  3. 2015 Multi-University Training Contest 3 1001 Magician

    Magician Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5316 Mean: n个数,2种操作,1是单点更新,2是询问区间 ...

  4. 回文树(回文自动机) - BZOJ 3676 回文串

    BZOJ 3676 回文串 Problem's Link: http://www.lydsy.com/JudgeOnline/problem.php?id=3676 Mean: 略 analyse: ...

  5. python 神经网络实例

    #http://python.jobbole.com/82758/ # import numpy as np # # # # sigmoid function # def nonlin(x, deri ...

  6. 002Maven_第一个Maven演示

    第一步. 首先建立Hello项目同时建立Maven约定的目录结构 Hello     --src     -----main     ----------java     ----------reso ...

  7. 对于try catch放在能够很好地处理例外的位置

    Exception有一个message属性.在使用catch的时候可以调用: Catch(IOException e){System.out.println(e.message())}; Catch( ...

  8. cpio -H newc参数详解

    -H format 其中个format可以是: ‘bin’ The obsolete binary format. (2147483647 bytes) ‘odc’ The old (POSIX.1) ...

  9. Android使用Intent实现拨打电话的动作

    使用Intent实现打电话的动作,我们须要在 AnroidMainfest.xml中增加通话权限,打开这个文件,在application节点的前面增加以下内容 <uses-permission ...

  10. jQuery 插件开发指南

    jQuery凭借其简洁的API,对DOM强大的操控性,易扩展性越来越受到web开发人员的喜爱,经常有人询问一些技巧,因此干脆写这么一篇文章给各位jQuery爱好者,算是抛砖引玉吧. 那么首先我们来简单 ...