zoukankan      html  css  js  c++  java
  • springMVC学习(9)-全局异常处理

    一、异常处理思路:

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

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

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

    二、自定义异常类 继承Exception

     1 package com.cy.exception;
     2 
     3 /**
     4  * 系统 自定义异常类,针对预期的异常,需要在程序中抛出此类的异常
     5  * @author chengyu
     6  *
     7  */
     8 public class CustomException extends Exception{
     9     
    10     public String message; 
    11     
    12     public CustomException(String message){
    13         super(message);
    14         this.message = message;
    15     }
    16 
    17     public String getMessage() {
    18         return message;
    19     }
    20 
    21     public void setMessage(String message) {
    22         this.message = message;
    23     }
    24 }
    CustomException

    三、全局异常处理器:

    思路:

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

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

    解析出异常类型

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

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

     1 package com.cy.exception;
     2 
     3 import javax.servlet.http.HttpServletRequest;
     4 import javax.servlet.http.HttpServletResponse;
     5 
     6 import org.springframework.web.servlet.HandlerExceptionResolver;
     7 import org.springframework.web.servlet.ModelAndView;
     8 
     9 /**
    10  * 全局异常处理器
    11  * @author chengyu
    12  *
    13  */
    14 public class CustomExceptionResolver implements HandlerExceptionResolver{
    15     
    16     /**
    17      * ex 系统 抛出的异常
    18      * handler就是处理器适配器要执行Handler对象(只有method)
    19      */
    20     @Override
    21     public ModelAndView resolveException(HttpServletRequest request,
    22             HttpServletResponse response, Object handler, Exception ex) {
    23         
    24         CustomException customException = null;
    25         
    26         //解析出异常类型
    27         //如果该 异常类型是系统 自定义的异常,直接取出异常信息,在错误页面展示
    28         //如果该 异常类型不是系统 自定义的异常,构造一个自定义的异常类型(信息为“未知错误”)
    29         if(ex instanceof CustomException){
    30             customException = (CustomException) ex;
    31         }else{
    32             customException = new CustomException("未知错误!");
    33         }
    34         
    35         ModelAndView modelAndView = new ModelAndView();
    36         modelAndView.addObject("message", customException.getMessage());
    37         modelAndView.setViewName("error");
    38         return modelAndView;
    39     }
    40 
    41 }
    View Code

    四、错误页面:

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>错误提示</title>
     8 </head>
     9 <body>
    10 ${message }
    11 </body>
    12 </html>
    View Code

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

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

    五、测试代码:

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

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

    1)在Controller中抛出异常:

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

    2)在service中抛出异常:

     1 @Override
     2     public ItemsCustom findItemsById(Integer id) throws Exception {
     3         Items items = itemsMapper.selectByPrimaryKey(id);
     4         
     5         if(items == null){
     6             throw new CustomException("该商品信息不存在啊!");
     7         }
     8         
     9         ItemsCustom itemsCoustom = null;
    10         if(items!=null){
    11             itemsCoustom = new ItemsCustom();
    12             BeanUtils.copyProperties(items, itemsCoustom);
    13         }
    14         
    15         return itemsCoustom;
    16     }
    View Code

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

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

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

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

  • 相关阅读:
    vue项目中使用mockjs模拟接口返回数据
    Node.js:Express 框架
    Node.js:Web模块、文件系统
    Node.js:get/post请求、全局对象、工具模块
    Node.js:常用工具、路由
    echarts使用记录(二)legend翻页,事件,数据集,设置y轴最大/小值,让series图形从右侧出往左移动
    Node.js:模块系统、函数
    ElementUI表单验证使用
    高级程序员职责
    Git:fatal: The remote end hung up unexpectedly
  • 原文地址:https://www.cnblogs.com/tenWood/p/6336936.html
Copyright © 2011-2022 走看看