异常处理思路
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>
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";
}
}
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>
配置好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;
}
}
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;
}
}
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>
3、配置异常处理器
在springmvc.xml里面进行配置:(添加如下配置信息)
<!--配置异常处理器对象-->
<bean id="sysExceptionResolver" class="com.keafmd.exception.SysExceptionResolver"/>
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>
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";
}
}
异常处理效果演示
运行效果:
点击testException后,显示我们自定义的异常信息。
以上就是SpringMVC的异常处理的全部内容。
看完如果对你有帮助,感谢点赞支持!
如果你是电脑端的话,看到右下角的 “一键三连” 了吗,没错点它[哈哈]
加油!
共同努力!
Keafmd