springMVC学习(9)-全局异常处理

时间:2023-03-10 02:35:25
springMVC学习(9)-全局异常处理

一、异常处理思路:

系统中异常包括两类:预期异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发、测试通过手段减少运行时异常的发生。

系统的dao、service、controller出现都通过throws Exception向上抛出,最后由springmvc前端控制器交由异常处理器进行异常处理,如下图:

springMVC学习(9)-全局异常处理

springmvc提供全局异常处理器(一个系统只有一个异常处理器)进行统一异常处理。

二、自定义异常类 继承Exception

 package com.cy.exception;

 /**
* 系统 自定义异常类,针对预期的异常,需要在程序中抛出此类的异常
* @author chengyu
*
*/
public class CustomException extends Exception{ public String message; public CustomException(String message){
super(message);
this.message = message;
} public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
}
}

CustomException

三、全局异常处理器:

思路:

系统遇到异常,在程序中手动抛出,dao抛给service、service给controller、controller抛给前端控制器,前端控制器调用全局异常处理器。

全局异常处理器处理思路:

解析出异常类型

如果该 异常类型是系统 自定义的异常,直接取出异常信息,在错误页面展示

如果该 异常类型不是系统 自定义的异常,构造一个自定义的异常类型(信息为“未知错误”)

 package com.cy.exception;

 import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView; /**
* 全局异常处理器
* @author chengyu
*
*/
public class CustomExceptionResolver implements HandlerExceptionResolver{ /**
* ex 系统 抛出的异常
* handler就是处理器适配器要执行Handler对象(只有method)
*/
@Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex) { CustomException customException = null; //解析出异常类型
//如果该 异常类型是系统 自定义的异常,直接取出异常信息,在错误页面展示
//如果该 异常类型不是系统 自定义的异常,构造一个自定义的异常类型(信息为“未知错误”)
if(ex instanceof CustomException){
customException = (CustomException) ex;
}else{
customException = new CustomException("未知错误!");
} ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message", customException.getMessage());
modelAndView.setViewName("error");
return modelAndView;
} }

四、错误页面:

 <%@ 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>
</head>
<body>
${message }
</body>
</html>

在springmvc.xml中配置全局异常处理器:

 <!-- 全局异常处理器 只要实现HandlerExceptionResolver接口就是全局异常处理器 -->
<bean class="com.cy.exception.CustomExceptionResolver" />

五、测试代码:

在controller、service、dao中任意一处手动抛出异常;手动抛出的异常,在错误页面显示自定义的错误异常信息;

如果不是手动抛出的异常,说明是一个RunTimeException;在错误页面只显示“未知错误”;

1)在Controller中抛出异常:

 @RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})
public String editItems(Model model,@RequestParam(value="id",required=true,defaultValue="4") Integer items_id) throws Exception{
ItemsCustom itemsCustom = itemsService.findItemsById(items_id); if(itemsCustom == null){
throw new CustomException("商品信息不存在!");
} model.addAttribute("items", itemsCustom); return "items/editItems";
}

2)在service中抛出异常:

 @Override
public ItemsCustom findItemsById(Integer id) throws Exception {
Items items = itemsMapper.selectByPrimaryKey(id); if(items == null){
throw new CustomException("该商品信息不存在啊!");
} ItemsCustom itemsCoustom = null;
if(items!=null){
itemsCoustom = new ItemsCustom();
BeanUtils.copyProperties(items, itemsCoustom);
} return itemsCoustom;
}

程序运行到抛出异常会跳出该方法的,不再执行;

springMVC学习(9)-全局异常处理

如果与业务功能相关的异常,建议在service中抛出异常。

与业务功能没有关系的异常,建议在controller中抛出。

上边的功能,建议在service中抛出异常。