springmvc--异常处理

1.1. 异常处理思路

       系统中异常包括两类:预期异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发、测试通过手段减少运行时异常的发生。

       系统的dao、service、controller出现都通过throws Exception向上抛出,最后由springmvc前端控制器交由异常处理器进行异常处理,如下图:

1.2. 自定义异常类

       为了区别不同的异常,通常根据异常类型进行区分,这里我们创建一个自定义系统异常。

如果controller、service、dao抛出此类异常说明是系统预期处理的异常信息。

 1 public class MyException extends Exception {
 2     // 异常信息
 3     private String message;
 4 
 5     public MyException() {
 6         super();
 7     }
 8 
 9     public MyException(String message) {
10         super();
11         this.message = message;
12     }
13 
14     public String getMessage() {
15         return message;
16     }
17 
18     public void setMessage(String message) {
19         this.message = message;
20     }
21 
22 }
View Code

1.3. 自定义异常处理器

 1 public class CustomHandleException implements HandlerExceptionResolver {
 2 
 3     @Override
 4     public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
 5             Exception exception) {
 6         // 判断异常类型        String msg;
 7 
 8         // 判断异常类型
 9         if (exception instanceof MyException) {
10             // 如果是自定义异常,读取异常信息
11             msg = exception.getMessage();
12         } else {
13             // 如果是运行时异常,则取错误堆栈,从堆栈中获取异常信息
14             Writer out = new StringWriter();
15             PrintWriter s = new PrintWriter(out);
16             exception.printStackTrace(s);
17             msg = out.toString();
18 
19         }
20 
21         // 把错误信息发给相关人员,邮件,短信等方式
22 
23         // 返回错误页面,给用户友好页面显示错误信息
24         ModelAndView modelAndView = new ModelAndView();
25         modelAndView.addObject("msg", msg);
26         modelAndView.setViewName("error");
27 
28         return modelAndView;
29     }
30 }
View Code

1.4. 异常处理器配置

在springmvc.xml中添加:

1 <!-- 全局异常处理器 -->
2     <bean id="myException" class="com.jove.exception.CustomHandleException"/>
View Code

1.5. 错误页面

 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: jove
 4   Date: 2018/2/4
 5   Time: 12:47
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 9 <html>
10 <head>
11     <title>Title</title>
12 </head>
13 <body>
14 <h1>exception</h1>
15 <p>${msg}</p>
16 </body>
17 </html>
View Code

1.6. 异常测试

修改ItemController方法“queryItemList”,抛出异常:

 1 /**
 2  * 查询商品列表
 3  * 
 4  * @return
 5  * @throws Exception
 6  */
 7 @RequestMapping(value = { "itemList", "itemListAll" })
 8 public ModelAndView queryItemList() throws Exception {
 9     // 自定义异常
10     if (true) {
11         throw new MyException("自定义异常出现了~");
12     }
13 
14     // 运行时异常
15     int a = 1 / 0;
16 
17     // 查询商品数据
18     List<Item> list = this.itemService.queryItemList();
19     // 创建ModelAndView,设置逻辑视图名
20     ModelAndView mv = new ModelAndView("itemList");
21     // 把商品数据放到模型中
22     mv.addObject("itemList", list);
23 
24     return mv;
25 }
View Code

 

posted on 2018-02-04 13:34  jovelove  阅读(128)  评论(0)    收藏  举报