• 09_springmvc异常处理


    一.异常处理思路

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

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

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

    二.自定义异常类

    对不同的异常类型定义异常类,继承Exception,针对预期的异常,需要在程序中抛出此类的异常。

    package com.ssm.exception;
    
    /**
     * Description:自定义异常类,继承Exception, 预期的异常程序中抛出异常
     * User: jiatp
     * Date: 2019/9/10 0010 下午 2:55
    */
    public class CustomException extends Exception{
    
        //异常信息
        public String message;
    
    
        public CustomException(String message){
            super(message);
            this.message = message;
        }
    
        @Override
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    }
    

    三.全局异常处理器

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

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

    解析出该异常;

    若该异常是系统自定义的异常,直接取出异常信息,在错误页面显示;

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

    springmvc提供一个HandlerExceptionResolver接口

    异常解析类:

    package com.ssm.exception;
    
    import org.springframework.web.servlet.HandlerExceptionResolver;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Description:异常信息解析类
     * User: jiatp
     * Date: 2019/9/10 0010 下午 3:13
    */
    public class CustomExceptionResolver implements HandlerExceptionResolver {
    
    
        @Override
        public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler, Exception e) {
          
            CustomException customException  = null;//判断是否是自定义的异常类
            if(e instanceof CustomException){
                customException = (CustomException)e;//如果是,异常转为customException
            }else{
                customException = new CustomException("未知错误");
    
            }
            //得到异常信息
            String message = customException.getMessage();
            //定义modelAndView
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("message",message);//将信息放入modelandview
            modelAndView.setViewName("error");
    
            return modelAndView;
        }
    }
    

    四.错误页面

    编写一个页面展示异常信息

    五.springmvc.xml配置全局异常处理器

    六.异常测试

    在controller、service、dao中任意一处需要手动抛出异常。

    如果是程序中手动抛出的异常,在错误页面中显示自定义的异常信息,如果不是手动抛出异常说明是一个运行时异常,在错误页面只显示“未知错误”。

    在商品修改的controller方法中抛出异常

    在service接口中抛出异常:

    异常测试:

    一般情况:

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

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

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

  • 相关阅读:
    地址栏访问Action,后来方法执行两次
    转:Android中的Selector的用法
    转:android 自定义RadioButton样式
    Android中@id与@+id区别
    INSTALL_PARSE_FAILED_MANIFEST_MALFORMED 错误
    Supervisor
    mysql 赋予权限连接
    定时任务
    git 提交代码五部曲
    Mysql 之事物
  • 原文地址:https://www.cnblogs.com/jatpeo/p/11767517.html
Copyright © 2020-2023  润新知