springmvc在处理请求过程中出现异常信息交由异常处理器进行处理,自定义异常处理器可以实现一个系统的异常处理逻辑。
1. 异常处理思路
系统中异常包括两类:预期异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发、测试通过手段减少运行时异常的发生。
系统的dao、service、controller出现都通过throws Exception向上抛出,最后由springmvc前端控制器交由异常处理器进行异常处理,如下图:
2. 自定义异常
为了区别不同的异常通常根据异常类型自定义异常类,这里我们创建一个自定义系统异常,如果controller、service、dao抛出此类异常说明是系统预期处理的异常信息。(自定义异常继承Exception类)
package cn.rodge.ssm.exception;
public class CustomerException extends Exception {
private static final long serialVersionUID = -3387516993124229948L;
private String expMessage;
public String getExpMessage() {
return expMessage;
}
public void setExpMessage(String expMessage) {
this.expMessage = expMessage;
}
public CustomerException(String expMessage) {
super();
this.expMessage = expMessage;
}
public CustomerException() {
super();
}
public CustomerException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public CustomerException(String message, Throwable cause) {
super(message, cause);
}
public CustomerException(Throwable cause) {
super(cause);
}
}
3. 自定义异常处理器
自定义异常处理器实现HandlerExceptionResolver接口
package cn.rodge.ssm.exception;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
public class GolbalException implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
Exception exception) {
exception.printStackTrace();
String msg = null;
ModelAndView modelAndView = new ModelAndView();
//1.判断异常种类
if (exception instanceof CustomerException) {
//2.如果是自定义异常,异常强制转换,并获取异常信息
CustomerException customerException = (CustomerException) exception;
msg = customerException.getExpMessage();
} else {
//3.如果是runtimeexception子类,获取异常信息
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
exception.printStackTrace(printWriter);
msg = stringWriter.toString();
}
modelAndView.addObject("msg", msg);
modelAndView.setViewName("error");
return modelAndView;
}
}
4. 配置错误页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>错误友好页面</h1>
<br>
<h2>异常信息:</h2>
<br>
${msg}
</body>
</html>
5. 异常处理器配置
在springmvc.xml中配置异常处理器
<!-- 配置全局异常处理器 -->
<bean class="cn.rodge.ssm.exception.GolbalException"></bean>
6. 异常测试
@RequestMapping("/itemList.action")
public String itemList (Model model) throws CustomerException {
//异常测试
if (true) {
// throw new CustomerException("我是自定义异常~~~~~~~~");
throw new RuntimeException("系统异常");
}
//调用业务层查询订单数据
List<Items> itemList = itemService.findAll();
//将查询结果保存在model中
model.addAttribute("itemList", itemList);
//页面转发
return "itemList";
}