SSM整合之异常处理之返回特定页面

异常处理思路

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

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

springmvc提供全局异常处理器进行统一的异常处理,一个系统只有一个一个异常处理器


自定义异常类

对不同的异常类型定义异常类,继承Exception。
[java]  view plain  copy
  1. /** 
  2.  * Created by Alex on 2017/6/29. 
  3.  * 系统自定义异常类 
  4.  */  
  5. public class CustomException extends Exception {  
  6.     //异常信息  
  7.     public String message;  
  8.     public CustomException(String message){  
  9.         super(message);  
  10.         this.message = message;  
  11.     }  
  12.   
  13.     @Override  
  14.     public String getMessage() {  
  15.         return message;  
  16.     }  
  17.   
  18.     public void setMessage(String message) {  
  19.         this.message = message;  
  20.     }  
  21. }  

全局异常处理器

思路:
系统遇到异常时,在程序中手动抛出, dao抛给serviceservice再抛给Controller最后Controller抛给前端控制器前端控制器调用全局异常处理器

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


springmvc提供一个HandlerExceptopnResolver

[java]  view plain  copy
  1. /** 
  2.  * Created by Alex on 2017/6/29. 
  3.  * 全局异常处理器 
  4.  */  
  5. public class CustomExceptionResolver implements HandlerExceptionResolver{  
  6.     /** 
  7.      * 系统抛出的异常 
  8.      * @param httpServletRequest 
  9.      * @param httpServletResponse 
  10.      * @param o 
  11.      * @param e 系统抛出的异常 
  12.      * @return 
  13.      */  
  14.     @Override  
  15.     public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {  
  16.        // 解析出异常类型  
  17.         CustomException customException = null;  
  18.         // 若该异常类型是系统自定义的异常,直接取出异常信息在错误页面展示即可。  
  19.         if(e instanceof CustomException){  
  20.             customException = (CustomException)e;  
  21.         }else{  
  22.             // 如果不是系统自定义异常,构造一个系统自定义的异常类型,信息为“未知错误”  
  23.             customException = new CustomException("未知错误");  
  24.         }  
  25.         //错误信息  
  26.         String message = customException.getMessage();  
  27.         ModelAndView modelAndView = new ModelAndView();  
  28.         //将错误信息传到页面  
  29.         modelAndView.addObject("message",message);  
  30.         //指向错误页面  
  31.         modelAndView.setViewName("error");  
  32.         return modelAndView;  
  33.     }  
  34. }  

错误页面

[html]  view plain  copy
  1. <%--  
  2.   Created by IntelliJ IDEA.  
  3.   User: Alex  
  4.   Date: 2017/6/29  
  5.   Time: 20:06  
  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>  
  12. </head>  
  13. <body>  
  14. ${message}  
  15. </body>  
  16. </html>  

springmvc.xml中的配置全局异常处理器

[html]  view plain  copy
  1. <!--  
  2. 全局异常处理器  
  3. 只要类实现了HandlerExceptionResolver接口,就是一个全局异常处理器哦  
  4. -->  
  5. <bean class="com.alex.ssm.exception.CustomExceptionResolver"/>  


异常测试

在controller,service,dao中任意一处需要手动抛出异常
如果是程序中手动抛出的异常,在错误页面中会显示自定义的异常信息,若不是手动抛出的异常,说明该异常是一个runtime异常,只显示“未知错误”。



在Controller里手动抛出异常
[java]  view plain  copy
  1. @RequestMapping(value = "/editItems",method = {RequestMethod.POST,RequestMethod.GET})  
  2. //@RequestParam 指定request传入的参数名,即可和形参进行绑定  
  3. // 通过required属性指定参数是否必须要传入  
  4. public String editItems(Model model, @RequestParam(value = "id",required = true)Integer items_id) throws Exception{  
  5.     //调用service 根据商品ID查询商品信息  
  6.     ItemsCustom itemsCustom = itemsService.findItemsById(items_id);  
  7.     //判断商品是否为空,抛出异常  
  8.     if(itemsCustom == null){  
  9.         throw new CustomException("修改的商品不存在!");  
  10.     }  
  11.     //通过形参中的model将model的数据传到页面  
  12.     //相当于modelandview的addObject  
  13.     model.addAttribute("items",itemsCustom);  
  14.     return "items/editItems";  
  15. }  

在Service接口中手动抛出异常
[java]  view plain  copy
  1. @Override  
  2. public ItemsCustom findItemsById(Integer id) throws Exception {  
  3.     Items items = itemsMapper.selectByPrimaryKey(id);  
  4.     if(items == null){  
  5.         throw new CustomException("修改的商品不存在!");  
  6.     }  
  7.   
  8.     //对于商品信息进行业务处理  
  9.     //....  
  10.     //最终返回ItemsCustom  
  11.     ItemsCustom itemsCustom = null;  
  12.     //将items的内容拷贝到itemsCustom  
  13.     if(items != null){  
  14.         itemsCustom = new ItemsCustom();  
  15.         BeanUtils.copyProperties(items,itemsCustom);  
  16.     }  
  17.     return itemsCustom;  
  18. }  



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

上面的功能建议在service中抛出异常



原博客:http://blog.csdn.net/fjnmbb12/article/details/73927539

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值