1、出现异常页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>异常处理</h3>
<a href="user/testException">异常处理</a>
</body>
</html>
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/testException")
public String testException(){
System.out.println("testException执行了...");
// 模拟异常
int a = 10/0;
return "success";
}
}
页面发送请求
出现错误页面
2、添加异常处理器
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/testException")
public String testException() throws SysException{
System.out.println("testException执行了...");
try {
// 模拟异常
int a = 10/0;
} catch (Exception e) {
// 打印异常信息
System.out.println("错误信息:"+e.getMessage());
// 抛出自定义异常信息
throw new SysException("查询所有用户出现错误了...");
}
return "success";
}
}
/**
* 自定义异常类
*/
public class SysException extends Exception{
// 存储提示信息的
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public SysException() {}
public SysException(String message) {
this.message = message;
}
}
/**
* 异常处理器
*/
public class SysExceptionResolver implements HandlerExceptionResolver{
/**
* 处理异常业务逻辑
* 方法形参Exception对象就是controller方法抛出的异常
*/
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception exception) {
// 获取到异常对象
SysException sysException;
if(exception instanceof SysException){
// 异常类型是自定义的异常类型
sysException = (SysException)exception;
}else{
// 其他异常类型
sysException = new SysException("系统正在维护....");
}
// 创建ModelAndView对象
ModelAndView mv = new ModelAndView();
// 获取到异常信息
mv.addObject("errorMsg",sysException.getMessage());
// 跳转到异常的页面
mv.setViewName("error");
return mv;
}
}
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">
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.liaoxiang"/>
<!-- 视图解析器对象 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--配置异常处理器-->
<bean id="sysExceptionResolver" class="com.liaoxiang.exception.SysExceptionResolver"/>
<!-- 开启SpringMVC框架注解的支持 -->
<mvc:annotation-driven />
</beans>
错误页面
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${errorMsg}
</body>
</html>
重新发送请求: