Spring MVC处理异常最基本的就是HandlerExceptionResolver这个接口,先看张图

分析上图可以轻松总结出,spring mvc里有三种异常处理方法:

1.使用官方提供的简单异常处理器SimpleMappingExceptionResolver

使用示例

springMVC-servlet.xml

<beanclass="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
定义默认的异常处理页面,当该异常类型的注册时使用
<property name="defaultErrorView" value="error">
</property>
定义异常处理页面用来获取异常信息的变量名,默认名为exception
<propertyname="exceptionAttribute"value="ex"></property>
定义需要特殊处理的异常,用类名或完全路径名作为key,异常也页名作为值
<property name="exceptionMappings">
<props>
<propkey="etc.exception.MyException">error-my</prop>
这里还可以继续扩展对不同异常类型的处理
</props>
</property>
</bean>

MyExceptionHandler.java

public class MyExceptionHandler{

    @ExceptionHandler
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex) {
Map<String, Exception> map = new HashMap<String, Exception>();
map.put("ex",ex); // 根据获取的Exception参数进行view跳转
if (ex instanceof MyException) {
return new ModelAndView("error-my",map);
} else {
return new ModelAndView("error",map);
}
}
}

定义了这样一个异常处理器之后就要在springMVC-servlet.xml中定义这样一个bean对象,如

<bean id="myExceptionHandler" class="com.tiantian.xxx.web.handler.myExceptionHandler"/>

2.使用@ResponseStatus注解

带有@ResponseStatus注解的异常类会被ResponseStatusExceptionResolver 解析。可以实现自定义的一些异常,同时在页面上进行显示。

使用示例

首先定义一个异常类:

@ResponseStatus(value = HttpStatus.FORBIDDEN,reason = "用户名和密码不匹配!")
public class UserNameNotMatchPasswordException extends RuntimeException{ }

抛出一个异常:

  @RequestMapping("/testResponseStatusExceptionResolver")
public String testResponseStatusExceptionResolver(@RequestParam("i") int i){
if (i==13){
throw new UserNameNotMatchPasswordException();
}
System.out.println("testResponseStatusExceptionResolver....");
return "success";
}

输入如下额路径:

http://localhost:8090/testResponseStatusExceptionResolver?i=13

当然,也可以在方法上进行修饰:

@ResponseStatus(reason = "测试",value = HttpStatus.NOT_FOUND)
@RequestMapping("/testResponseStatusExceptionResolver")
public String testResponseStatusExceptionResolver(@RequestParam("i") int i){
if (i==13){
throw new UserNameNotMatchPasswordException();
}
System.out.println("testResponseStatusExceptionResolver....");
return "success";
}

这时所有的请求都会报错。

3.使用全局异常处理

@ExceptionHandler异常统一处理

SpringMVC中异常处理详解的更多相关文章

  1. Spring Boot异常处理详解

    在Spring MVC异常处理详解中,介绍了Spring MVC的异常处理体系,本文将讲解在此基础上Spring Boot为我们做了哪些工作.下图列出了Spring Boot中跟MVC异常处理相关的类 ...

  2. SpringMVC 常用注解 详解

    SpringMVC 常用注解 详解 SpringMVC 常用注解 1.@RequestMapping                                      路径映射 2.@Requ ...

  3. PHP异常处理详解

      PHP异常处理详解 异常处理(又称为错误处理)功能提供了处理程序运行时出现的错误或异常情况的方法. 异常处理通常是防止未知错误产生所采取的处理措施.异常处理的好处是你不用再绞尽脑汁去考虑各种错误, ...

  4. Java程序在内存中运行详解

    目录 Java程序在内存中运行详解 一.JVM的内存分布 二.程序执行的过程 三.只有一个对象时的内存图 四.两个对象使用同一个方法的内存图 五.两个引用指向同一个对象的内存图 六.使用对象类型作为方 ...

  5. Web容器中DefaultServlet详解 JspServlet DefaultServlet

    Web容器中DefaultServlet详解 https://blog.csdn.net/qq_30920821/article/details/78328608 Web容器中DefaultServl ...

  6. Web容器中DefaultServlet详解

    万分感谢原文作者:_licho 原文链接:https://blog.csdn.net/qq_30920821/article/details/78328608 Web容器中DefaultServlet ...

  7. winxp计算机管理中服务详解

    winxp计算机管理中服务详解01 http://blog.sina.com.cn/s/blog_60f923b50100efy9.html http://blog.sina.com.cn/s/blo ...

  8. cocos2dx常见的46中+22中动作详解

    cocos2dx常见的46中+22中动作详解 分类: iOS2013-10-16 00:44 1429人阅读 评论(0) 收藏 举报 bool HelloWorld::init(){    ///// ...

  9. Android中Context详解 ---- 你所不知道的Context

    转自:http://blog.csdn.net/qinjuning/article/details/7310620Android中Context详解 ---- 你所不知道的Context 大家好,  ...

随机推荐

  1. java 正则表达式获得html字符串中<img src>中的src中的url地址

    public static Set<String> getImgStr(String htmlStr) { Set<String> pics = new HashSet< ...

  2. 没有main函数的helloworld

    差点儿全部程序猿的第一堂课都是学习helloworld程序,以下我们先来重温一下经典的C语言helloworl /* hello.c */ #include <stdio.h> int m ...

  3. css选择器顺序的小技巧

    在线演示 本地下载 css的选择器的顺序其实很有意思,如果应用的好的话,可以做一些简单的逻辑出来,配合上css3,就可以尽可能的脱离js了. 这里的演示是一个带有hover事件的四张照片,效果来自一个 ...

  4. ThinkPHP3.2 新bug ReadHtmlCache 支持不区分大写和小写的函数

    报错提示: Fatal error: Function name must be a string in D:\wwwroot\zbphp.com\ThinkPHP\Library\Behavior\ ...

  5. ZOJ 2319 Beautiful People

    LIS.先按S降序升序再按B降序排序(如果B不按降序排序的话就会覆盖掉正解),然后再对B用O(nlog(n))的LIS求解就可以了.用d数组标记每个元素在上升序列中的位置,然后根据d倒着找id就可以了 ...

  6. 微信小程序 - 各种示例

    示例更新(后续持续更新): 最后一次更新:2018-11-7 小程序-地图显示(调用高德地图.腾讯App) - (2018-11-1) 小程序-上传图片(上传.预览.展示.删除) - (2018-11 ...

  7. Oracle体系结构二(学习笔记)

  8. 王立平--include在Android中的应用

    一个布局中包括还有一个布局 1.在layout下定义activity_other.xml布局 2.代码中的包括例如以下: <LinearLayout xmlns:android="ht ...

  9. chrome插件的popup与跨域请求

    chrome插件的popup与跨域请求 tkorays <tkorays@hotmail.com> popup及其它js脚本         在chrome插件开发中,大致有几种类型的js ...

  10. 〖Linux〗zigbee实验之cc2430的cc debugger固件升级实录

    开发环境:Windows XP 1. (Trouble)一开始,使用IAR提示此设备不可使用(意味着无法下载程序): 2. (Search)通过一番的仔细查找,发现是cc debugger的Evalu ...