SpringMVC4.3x教程之八异常的三种处理方式
来源:互联网 发布:男神的偶像知乎 编辑:程序博客网 时间:2024/05/31 06:24
SpirngMVC提供了对异常的捕获,常用的有三种方式:
1、自带的异常解析类
2、自定义异常解析类
3、使用注解实现异常解析捕获
SpringMVC在运行中的异常都是从下往上抛出的最后到达ExceptionResolver进行异常的处理,但是前端控制器没有给出该异常解析器的默认对象,想要使用那么就需要进行实现。
控制器:
//异常处理控制器@Controllerpublic class ExceptionController { // 主动出现异常,演示异常处理 @RequestMapping("/err1") public String test1() { Random random = new Random(); if (random.nextInt(2) == 0) { throw new ArrayIndexOutOfBoundsException("亲,崩了"); } else { throw new NullPointerException("亲,我出去旅个游"); } } }
错误页面:
error
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>出错啦,绩效又没了</title><style type="text/css">.dv{color:red;}</style></head><body><h1>错误信息</h1><div class="dv">${ex.getMessage() }</div></body></html>
nullError页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>空指针搞不到就回家</title><style type="text/css">.dv{color:red;}</style></head><body><h1>错误信息</h1><div class="dv">${ex.getMessage() }</div></body></html>
第一种:使用SimpleMappingExceptionResolver
配置文件:
<!-- 第一种方式 --> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <!--配置显示异常的错误页面 --> <property name="defaultErrorView" value="error"></property> <!--配置异常对象名称,可以不配置,默认exception --> <property name="exceptionAttribute" value="ex"></property> <!--如果想根据不同的异常,设置对应要显示错误的页面 --> <property name="exceptionMappings"> <props> <!--key为异常类 值为错误页面 --> <prop key="java.lang.NullPointerException">nullError</prop> </props> </property> </bean>
这样就可以了直接访问我们的控制器err1
第二种:使用HandlerExceptionResolver接口
自定义全局异常解析器:
//全局异常处理的第二种方式public class SecondException implements HandlerExceptionResolver{ @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) { // TODO Auto-generated method stub System.out.println("第二种异常捕获:"); ModelAndView mv=new ModelAndView(); mv.addObject("ex", exception);//将产生的异常对象添加到Model mv.setViewName("error"); return mv; }}
在配置中进行配置:
<!-- 异常处理的第二种方式 --><bean class="org.qf.web.exception.SecondException"></bean>
第三种:使用注解 @ExceptionHandler
需要写在控制器中
控制器:
//异常处理控制器@Controllerpublic class ExceptionController { // 第一种异常处理和第二种异常处理 @RequestMapping("/err1") public String test1() { Random random = new Random(); if (random.nextInt(2) == 0) { throw new ArrayIndexOutOfBoundsException("亲,崩了"); } else { throw new NullPointerException("亲,我出去旅个游"); } } //异常处理方法,只能处理当前控制器类内部产生的异常,而且异常的类型必须为指定类 @ExceptionHandler(value = { Exception.class }) public ModelAndView test3(Exception ex) { System.out.println("注解实现异常的捕获"); ModelAndView mv=new ModelAndView(); mv.addObject("ex", ex); mv.setViewName("error"); return mv; }}
这个就不需要配置了。
阅读全文
0 0
- SpringMVC4.3x教程之八异常的三种处理方式
- SpringMVC4.3x教程之三拦截器的使用
- SpringMVC4.3x教程之七国际化的三种实现详解
- SpringMVC4.3x教程之二常用注解说明
- SpringMVC4.3x教程之六验证Validator详解
- SpringMVC4.3x教程之九RESTFul使用
- SpringMVC4.3x教程之五SpringMVC表单标签的使用
- SpringMVC异常处理的三种方式
- SpringMVC4异常处理
- SpringMVC4异常处理
- log4j-1.x之三【web整合springmvc4】
- Springmvc中异常处理的三种方式
- Spring全局异常处理的三种方式
- ios-Swift处理异常的三种方式
- springMVC三种异常处理方式
- SpringMVC4.3x教程之一基本使用详解
- java之八 异常处理
- SpringBoot初始教程之统一异常处理(三)
- HDU4704Sum 费马小定理+大数取模
- Class……not found (ONETHINK框架)
- PAT 乙级 1019.数字黑洞(20)
- 4.1.2—栈—Longest Valid Parentheses
- android audio volume
- SpringMVC4.3x教程之八异常的三种处理方式
- 4.1.3—栈—Largest Rectangle in Histogram
- TypeError: cannot perform reduce with flexible type
- 5.1 Tensorflow:图与模型的加载与存储
- docker
- select和epoll的区别
- 4.1.4—栈—Evaluate Reverse Polish Notation
- impala保存时间类型timestamp---NanoTimeUtils根据JdateTime生成nanotime
- xshell5 的账号密码搬家