SpringMVC的异常处理(超详细)

举报
牛哄哄的柯南 发表于 2021/05/26 14:43:22 2021/05/26
【摘要】 SpringMVC的异常处理 异常处理思路异常处理先看下项目的目录结构演示程序异常演示效果 异常处理步骤1、自定义异常类2、自定义异常处理器3、配置异常处理器异常处理效果演示 异常处理思路 Controller调用service,service调用dao,异常都是向上抛出的,最终有DispatcherServlet找异常处理器进行异常的处...

异常处理思路

Controller调用service,service调用dao,异常都是向上抛出的,最终有DispatcherServlet找异常处理器进行异常的处理。

在这里插入图片描述

异常处理

先看下项目的目录结构

在这里插入图片描述

演示程序异常

index.jsp:

<%--
  Created by IntelliJ IDEA.
  User: Keafmd
  Date: 2021/2/6
  Time: 10:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head> <title>Title</title>
</head>
<body> <h1>异常处理</h1> <a href="user/testException">testException</a>

</body>
</html>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

UserController类:

package com.keafmd.controller;

import com.keafmd.exception.SysException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.sql.SQLOutput;

/**
 * Keafmd
 *
 * @ClassName: UserController
 * @Description:
 * @author: 牛哄哄的柯南
 * @date: 2021-02-06 10:14
 */
@Controller
@RequestMapping("/user")
public class UserController { @RequestMapping("testException") public String testException() throws Exception{ System.out.println("testException执行了。。。"); //模拟异常 int a = 10/0; return "success"; }

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

success.jsp:

<%--
  Created by IntelliJ IDEA.
  User: Keafmd
  Date: 2021/1/30
  Time: 15:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head> <title>Title</title>
</head>
<body> <h1>执行成功</h1>
</body>
</html>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

配置好tomcat后开始执行。

演示效果

在这里插入图片描述

点击testException后

在这里插入图片描述

这种显示非常不友好,我们需要处理下异常。

异常处理步骤

1、自定义异常类

SysException:

package com.keafmd.exception;

/**
 * Keafmd
 *
 * @ClassName: SysException
 * @Description: 自定义异常类
 * @author: 牛哄哄的柯南
 * @date: 2021-02-06 10:25
 */
public class SysException extends Exception{ //存储提示信息 private  String message; public SysException(String message) { this.message = message; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; }

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

2、自定义异常处理器

SysExceptionResolver:

package com.keafmd.exception;

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

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

/**
 * Keafmd
 *
 * @ClassName: SysExceptionResolver
 * @Description: 异常处理器
 * @author: 牛哄哄的柯南
 * @date: 2021-02-06 10:32
 */
public class SysExceptionResolver implements HandlerExceptionResolver { /** * 处理异常的业务逻辑 * @param httpServletRequest * @param httpServletResponse * @param o * @param e * @return */ @Override public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) { //准备获取到异常对象 SysException se = null; if(e instanceof SysException){ se = (SysException)e; }else{ se = new SysException("系统正在维护,请联系管理员。。。"); } //创建ModelAndView对象 ModelAndView mv = new ModelAndView(); mv.addObject("errorMsg",se.getMessage()); mv.setViewName("error"); return mv; }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

error.jsp:

<%--
  Created by IntelliJ IDEA.
  User: Keafmd
  Date: 2021/2/6
  Time: 10:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head> <title>Title</title>
</head>
<body> ${errorMsg}

</body>
</html>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

3、配置异常处理器

在springmvc.xml里面进行配置:(添加如下配置信息)

<!--配置异常处理器对象-->
<bean id="sysExceptionResolver" class="com.keafmd.exception.SysExceptionResolver"/>

  
 
  • 1
  • 2

springmvc.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 开启注解扫描,配置spring创建容器时要扫描的包 --> <context:component-scan base-package="com.keafmd"></context:component-scan> <!-- 配置视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--告诉前端控制器,哪些静态资源,不拦截--> <mvc:resources location="/css/" mapping="/css/**" /> <mvc:resources location="/images/" mapping="/images/**" /> <mvc:resources location="/js/" mapping="/js/**" /> <!-- 开启SpringMVC框架注解支持,注解配置spring开启注解mvc的支持 --> <mvc:annotation-driven/> <!--配置异常处理器对象--> <bean id="sysExceptionResolver" class="com.keafmd.exception.SysExceptionResolver"/>

</beans>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

UserController类(包含异常处理):

package com.keafmd.controller;

import com.keafmd.exception.SysException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.sql.SQLOutput;

/**
 * Keafmd
 *
 * @ClassName: UserController
 * @Description:
 * @author: 牛哄哄的柯南
 * @date: 2021-02-06 10:14
 */
@Controller
@RequestMapping("/user")
public class UserController { @RequestMapping("testException") public String testException() throws Exception{ System.out.println("testException执行了。。。"); try { //模拟异常 int a = 10/0; } catch (Exception e) { //控制台打印异常信息 e.printStackTrace(); //抛出自定义异常信息 throw new SysException("查询所有用户出现错误了。。"); } return "success"; }

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

异常处理效果演示

运行效果:
在这里插入图片描述

点击testException后,显示我们自定义的异常信息。

在这里插入图片描述

以上就是SpringMVC的异常处理的全部内容。

看完如果对你有帮助,感谢点赞支持!
如果你是电脑端的话,看到右下角的 “一键三连” 了吗,没错点它[哈哈]

在这里插入图片描述

加油!

共同努力!

Keafmd

文章来源: keafmd.blog.csdn.net,作者:牛哄哄的柯南,版权归原作者所有,如需转载,请联系作者。

原文链接:keafmd.blog.csdn.net/article/details/113726203

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。