SpringMVC-HMday02-03-异常处理
1 处理异常的思路
- 系统中异常包括两类:预期异常和运行时异常 RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发、通过测试手段减少运行时异常的发生。系统的 dao、service、controller 出现都通过 throws Exception 向上抛出,最后由 springmvc 前端
控制器交由异常处理器进行异常处理,如下图:


2 直接抛出异常到用户页面
2.1 搭建环境—创建module

2.2 编写index.js文件
<h3>直接抛出异常处理</h3>
<a href="exce/testException">直接抛出异常到用户页面</a>
2.3 编辑核心配置文件
<?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.zzy"/>
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/images/**" location="/images/"/>
<mvc:annotation-driven/>
</beans>
2.4 编辑控制器ExceptionController
package com.zzy.controller;
import com.zzy.exception.SySException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/exce")
public class ExceptionController {
@RequestMapping("/testException")
public String testException() throws SySException {
System.out.println("testException这个方法执行了。。。");
int a = 1 / 0;
return "success";
}
}
2.5 运行服务器访问


3 捕获处理异常友好提示
3.1 编辑index.jsp文件
<h3>捕获处理异常友好提示处理</h3>
<a href="exce/testException1">捕获处理异常友好提示</a>
3.2 编辑Exception1Controller
package com.zzy.controller;
import com.zzy.exception.SySException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/exce")
public class Exception1Controller {
@RequestMapping("/testException1")
public String testException() throws SySException {
System.out.println("testException1这个方法执行了。。。");
try {
int a = 1/0;
} catch (Exception e) {
e.printStackTrace();
throw new SySException("查询所有用户信息出现了错误");
}
return "success";
}
}
3.3 编辑自定义的异常类SySException—做提示信息
package com.zzy.exception;
public class SySException extends Exception{
private String message;
public SySException() {
}
public SySException(String message) {
this.message = message;
}
@Override
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String toString() {
return "SySException{" +
"message='" + message + '\'' +
'}';
}
}
3.4 编辑异常处理器SySExceptionHandler
package com.zzy.exception;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SySExceptionHandler implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
Object o, Exception e) {
SySException syse = null;
if (e instanceof SySException){
syse = (SySException) e;
}else {
syse = new SySException("系统正在维护。。。。");
}
ModelAndView mv = new ModelAndView();
mv.addObject("errorMsg", syse.getMessage());
mv.setViewName("error");
return mv;
}
}
3.5 编辑error.jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--获取ModelAndView中的数据--%>
${errorMsg}
</body>
</html>
3.6 配置异常处理器—在核心配置文件中配置
<bean id="sySExceptionHandler" class="com.zzy.exception.SySExceptionHandler"/>
3.7 运行服务器访问

