【SpringMVC整合MyBatis】springmvc异常处理-全局异常处理器开发

来源:互联网 发布:软件开发文档模板 编辑:程序博客网 时间:2024/06/07 05:47
异常处理
1.异常处理思路

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



springmvc提供全局异常处理器(一个系统只有一个异常处理器)进行统一异常处理。

2.自定义异常类

对不同的异常类型定义异常类,继承Exception。
[java] view plain copy
  1. package cn.edu.hpu.ssm.exception;  
  2.   
  3. //系统自定义异常处理类,针对预期的异常,需要在程序中抛出此类的异常  
  4. public class CustomException extends Exception{  
  5.       
  6.     //异常信息  
  7.     private String message;  
  8.       
  9.     public CustomException(String message){  
  10.         super(message);  
  11.         this.message=message;  
  12.     }  
  13.   
  14.   
  15.     public String getMessage() {  
  16.         return message;  
  17.     }  
  18.   
  19.   
  20.     public void setMessage(String message) {  
  21.         this.message = message;  
  22.     }  
  23. }  

3.全局异常处理器

思路:
系统遇到异常,在程序中手动抛出,dao抛给service、service给controller、controller抛给前端控制器,前端控制器调用全局异常处理器。

全局异常处理器处理思路:
    解析出异常类型。
    如果该 异常类型是系统 自定义的异常,直接取出异常信息,在错误页面展示。
    如果该 异常类型不是系统 自定义的异常,构造一个自定义的异常类型(信息为“未知错误”)。

springmvc提供一个HandlerExceptionResolver接口
[java] view plain copy
  1. package cn.edu.hpu.ssm.exception;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4. import javax.servlet.http.HttpServletResponse;  
  5.   
  6. import org.springframework.web.servlet.HandlerExceptionResolver;  
  7. import org.springframework.web.servlet.ModelAndView;  
  8.   
  9. //全局异常处理器  
  10. public class CustomExceptionResolver implements HandlerExceptionResolver{  
  11.   
  12.     //系统抛出的异常  
  13.     @Override  
  14.     public ModelAndView resolveException(HttpServletRequest request,  
  15.             HttpServletResponse response, Object handler, Exception ex) {  
  16.         //handler就是处理器适配器要执行的Handler对象(只有method)  
  17.         //解析出异常类型。  
  18.           
  19.         //如果该 异常类型是系统 自定义的异常,直接取出异常信息,在错误页面展示。  
  20.         CustomException customException=null;  
  21.         if(ex instanceof CustomException){  
  22.             customException=(CustomException)ex;  
  23.               
  24.         }else{  
  25.             //如果该 异常类型不是系统 自定义的异常,构造一个自定义的异常类型(信息为“未知错误”)。  
  26.             customException=new CustomException("未知错误");  
  27.         }  
  28.   
  29.         //错误信息  
  30.         String message=customException.getMessage();  
  31.           
  32.         ModelAndView modelAndView=new ModelAndView();  
  33.           
  34.         //将错误信息传到页面  
  35.         modelAndView.addObject("message",message);  
  36.           
  37.         //指向到错误界面  
  38.         modelAndView.setViewName("error");  
  39.           
  40.         return modelAndView;  
  41.     }  
  42.        
  43. }  

4.错误页面
在WEB-INF/jsp文件夹下创建error.jsp页面,内容为:
[java] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>错误提示</title>  
  13.   
  14.   
  15.   </head>  
  16.     
  17.   <body>  
  18.     <h1><font color="red">${message }</font></h1><br>  
  19.   </body>  
  20. </html>  

5.在springmvc.xml配置全局异常处理器
[html] view plain copy
  1. <!-- 全局异常处理器   
  2.     只要你实现了HandlerExceptionResolver接口,这个  
  3.     类就是一个全局异常处理器-->  
  4.     <bean class="cn.edu.hpu.ssm.exception.CustomExceptionResolver"></bean>  

6.异常测试
在controller、service、dao中任意一处需要手动抛出异常。
如果是程序中手动抛出的异常,在错误页面中显示自定义的异常信息,如果不是手动抛出异常说明是一个运行时异常,在错误页面只显示“未知错误”。

在商品修改的controller方法中抛出异常。
[java] view plain copy
  1. @RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})  
  2. //@RequestParam里面指定reuqest传入参数和形参进行绑定。  
  3. //通过required属性指定参数是否必须要传入  
  4. //通过defaultValue可以设置默认值,如果id参数没有传入,将默认值和形参绑定  
  5.     public String editItems(Model model,@RequestParam(value="id",required=true,defaultValue="") Integer items_id)throws Exception{  
  6.           
  7.         //调用service根据商品id查询商品信息  
  8.         ItemsCustom itemsCustom=itemsService.findItemsById(items_id);  
  9.         //判断商品是否为空,根据id没有查到商品,提示用户商品信息并不存在  
  10.         if(itemsCustom==null){  
  11.             throw new CustomException("商品的修改信息不存在!");  
  12.         }  
  13.           
  14.         //通过形参中的model将model数据传到页面  
  15.         //相当于modelAndView.addObject方法  
  16.         model.addAttribute("items22",itemsCustom);  
  17.           
  18.         return "items/editItems";  
  19.     }  

其中上面用到的service方法:
[java] view plain copy
  1. @Override  
  2. public ItemsCustom findItemsById(Integer id) throws Exception {  
  3.       
  4.     Items items=itemsMapper.selectByPrimaryKey(id);  
  5.     //中间对商品信息进行业务处理  
  6.     //...  
  7.     //最终返回ItemsCustom  
  8.     ItemsCustom itemsCustom=null;  
  9.     //将item的内容拷贝到itemsCustom  
  10.     if(items!=null){  
  11.         itemsCustom=new ItemsCustom();  
  12.         BeanUtils.copyProperties(items, itemsCustom);  
  13.     }  
  14.       
  15.     return itemsCustom;  
  16. }  

我们让id指定一个没有的数(如4444),则会抛出我们自定义的异常信息:

同样在Service中也可以抛出异常

如果与业务功能相关的异常,建议在service中抛出异常。
与业务功能没有关系的异常,建议在controller中抛出。

上边的功能,建议在service中抛出异常。

原创粉丝点击