zoukankan      html  css  js  c++  java
  • Springmvc 异常处理

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

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

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

    1.1      自定义异常类

    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;
        }
    }

    1.2     全局异常处理器

    思路:

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

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

                      解析出异常类型

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

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

    package cn.itcast.ssm.exception;

    public
    class CustomExceptionResolver implements HandlerExceptionResolver { @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { //handler就是处理器适配器要执行Handler对象(只有method) // 解析出异常类型 // 如果该 异常类型是系统 自定义的异常,直接取出异常信息,在错误页面展示 CustomException customException = null; if(ex instanceof CustomException){ customException = (CustomException)ex; }else{ customException = new CustomException("未知错误"); } //错误信息 String message = customException.getMessage(); ModelAndView modelAndView = new ModelAndView(); //将错误信息传到页面 modelAndView.addObject("message", message); //指向错误页面 modelAndView.setViewName("error"); return modelAndView; } }

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

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

    1.4      异常测试

    controller方法中抛出异常 .

        @RequestMapping(value = "/editItems", method = { RequestMethod.POST,
                RequestMethod.GET })
        // @RequestParam里边指定request传入参数名称和形参进行绑定。
        // 通过required属性指定参数是否必须要传入
        // 通过defaultValue可以设置默认值,如果id参数没有传入,将默认值和形参绑定。
        public String editItems(Model model,
                @RequestParam(value = "id", required = true) Integer items_id)
                throws Exception {
    
            // 调用service根据商品id查询商品信息
            ItemsCustom itemsCustom = itemsService.findItemsById(items_id);
            //判断商品是否为空,根据id没有查询到商品,抛出异常,提示用户商品信息不存 在
            if(itemsCustom == null){
                throw new CustomException("修改的商品信息不存在!");
            }
    
            // 通过形参中的model将model数据传到页面
            // 相当于modelAndView.addObject方法
            model.addAttribute("items", itemsCustom);
    
            return "items/editItems";
        }

    在service接口中抛出异常:

        @Override
        public ItemsCustom findItemsById(Integer id) throws Exception {
            Items items = itemsMapper.selectByPrimaryKey(id);
            if(items==null){
                throw new CustomException("修改的商品信息不存在!");
            }
            //中间对商品信息进行业务处理
            //....
            //返回ItemsCustom
            ItemsCustom itemsCustom = null;
            //将items的属性值拷贝到itemsCustom
            if(items!=null){
                itemsCustom = new ItemsCustom();
                BeanUtils.copyProperties(items, itemsCustom);
            }    
            return itemsCustom;
        }

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

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

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

  • 相关阅读:
    [TED]丹·吉尔伯特:我们为什么快乐?
    GTD 工具软件
    康奈尔笔记法(5R笔记法)
    ACM退役感言
    YCSB_对mongodb进行性能测试
    Python_列表比较大小
    Python_issubclass&isinstance方法和types&inspect内置模块
    由于未能创建 Microsoft Visual C# 2008 编译器,因此未能打开项目 "xxx" 的解决方法
    北京技能视频学习自动播放
    JQuery在UpdatePanel中当事件点击后JQuery事件不起作用的解决办法(JQuery与UpdatePanel问题解析)
  • 原文地址:https://www.cnblogs.com/zcjyzh/p/11722812.html
Copyright © 2011-2022 走看看