Spring MVC配置全局异常处理器!!!

为什么要使用全局异常处理器:如果不加以异常处理,错误信息肯定会抛在浏览器页面上,这样很不友好,所以必须进行异常处理。

异常处理思路

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

创建异常处理器:GlobalException:

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.exception;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>Project: SpringMVC - GlobleException</p>
 * <p>Powered by scl On 2024-01-10 14:09:52</p>
 * <p>描述:<p>
 *
 * @author 孙臣龙 [1846080280@qq.com]
 * @version 1.0
 * @since 17
 */
@Component
public class GlobalException implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        /**
         * 1.发邮件、发信息
         * 2.跳转到错误页面
         */
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg",e.getMessage());
        modelAndView.setViewName("exception");

        return modelAndView;
    }
}

  编写controller:ExceptionController:

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * <p>Project: SpringMVC - execptionController</p>
 * <p>Powered by scl On 2024-01-10 14:14:44</p>
 * <p>描述:<p>
 *
 * @author 孙臣龙 [1846080280@qq.com]
 * @version 1.0
 * @since 17
 */
@Controller
@RequestMapping("/account")
public class ExceptionController {

    @RequestMapping("/findException")
    public String findException() throws Exception{
        System.out.println(4/0);
        return "exception";
    }
}

在index.jsp里面定义超链接: 

  <a href="/account/findException">全局异常处理器</a>

结果展示:

 项目结构:

 

项目配置可参考:Spring MVC文件上传!!!-CSDN博客

相关推荐

  1. 后端异常处理:全局异常处理器

    2024-01-11 15:22:03       47 阅读
  2. SpringMVC异常处理

    2024-01-11 15:22:03       65 阅读
  3. SpringMVC异常处理

    2024-01-11 15:22:03       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-11 15:22:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-11 15:22:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-11 15:22:03       82 阅读
  4. Python语言-面向对象

    2024-01-11 15:22:03       91 阅读

热门阅读

  1. Elasticsearch安装IK分词器踩坑记录

    2024-01-11 15:22:03       61 阅读
  2. 【Linux】journalctl和dmesg日志的区别

    2024-01-11 15:22:03       50 阅读
  3. c++ 正则化

    2024-01-11 15:22:03       41 阅读
  4. PyTorch项目源码学习(1)

    2024-01-11 15:22:03       60 阅读
  5. Vue怎么设置自定义指令

    2024-01-11 15:22:03       51 阅读
  6. Vue组件

    Vue组件

    2024-01-11 15:22:03      55 阅读
  7. PHP对象设计(《深入PHP》第六章内容笔记)

    2024-01-11 15:22:03       57 阅读
  8. Vue3的 响应式数据

    2024-01-11 15:22:03       53 阅读
  9. 金三银四-JVM核心知识高频面试题

    2024-01-11 15:22:03       47 阅读
  10. Leetcode 437. Path Sum III (二叉树遍历好题)

    2024-01-11 15:22:03       53 阅读
  11. 【响应式编程】前置知识和相关技术的总结

    2024-01-11 15:22:03       51 阅读