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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章