異常處理思路
系統中異常包括兩類:預期異常和運行時異常RuntimeException,前者通過捕獲異常從而獲取異常信息,後者主要通過規範代碼開發、測試通過手段減少運行時異常的發生。
系統的dao、service、controller出現都通過throws Exception向上拋出,最後由springmvc前端控制器交由異常處理器進行異常處理,如下圖:
springmvc提供全局異常處理器進行統一的異常處理,一個系統只有一個一個異常處理器
自定義異常類
- /**
- * Created by Alex on 2017/6/29.
- * 系統自定義異常類
- */
- public class CustomException extends Exception {
- //異常信息
- public String message;
- public CustomException(String message){
- super(message);
- this.message = message;
- }
- @Override
- public String getMessage() {
- return message;
- }
- public void setMessage(String message) {
- this.message = message;
- }
- }
全局異常處理器
思路:
全局異常處理器處理思路:
springmvc提供一個HandlerExceptopnResolver
- /**
- * Created by Alex on 2017/6/29.
- * 全局異常處理器
- */
- public class CustomExceptionResolver implements HandlerExceptionResolver{
- /**
- * 系統拋出的異常
- * @param httpServletRequest
- * @param httpServletResponse
- * @param o
- * @param e 系統拋出的異常
- * @return
- */
- @Override
- public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
- // 解析出異常類型
- CustomException customException = null;
- // 若該異常類型是系統自定義的異常,直接取出異常信息在錯誤頁面展示即可。
- if(e instanceof CustomException){
- customException = (CustomException)e;
- }else{
- // 如果不是系統自定義異常,構造一個系統自定義的異常類型,信息爲“未知錯誤”
- customException = new CustomException("未知錯誤");
- }
- //錯誤信息
- String message = customException.getMessage();
- ModelAndView modelAndView = new ModelAndView();
- //將錯誤信息傳到頁面
- modelAndView.addObject("message",message);
- //指向錯誤頁面
- modelAndView.setViewName("error");
- return modelAndView;
- }
- }
錯誤頁面
- <%--
- Created by IntelliJ IDEA.
- User: Alex
- Date: 2017/6/29
- Time: 20:06
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>失敗!</title>
- </head>
- <body>
- ${message}
- </body>
- </html>
springmvc.xml中的配置全局異常處理器
- <!--
- 全局異常處理器
- 只要類實現了HandlerExceptionResolver接口,就是一個全局異常處理器哦
- -->
- <bean class="com.alex.ssm.exception.CustomExceptionResolver"/>
異常測試
在Controller裏手動拋出異常
- @RequestMapping(value = "/editItems",method = {RequestMethod.POST,RequestMethod.GET})
- //@RequestParam 指定request傳入的參數名,即可和形參進行綁定
- // 通過required屬性指定參數是否必須要傳入
- public String editItems(Model model, @RequestParam(value = "id",required = true)Integer items_id) throws Exception{
- //調用service 根據商品ID查詢商品信息
- ItemsCustom itemsCustom = itemsService.findItemsById(items_id);
- //判斷商品是否爲空,拋出異常
- if(itemsCustom == null){
- throw new CustomException("修改的商品不存在!");
- }
- //通過形參中的model將model的數據傳到頁面
- //相當於modelandview的addObject
- model.addAttribute("items",itemsCustom);
- return "items/editItems";
- }
在Service接口中手動拋出異常
- @Override
- public ItemsCustom findItemsById(Integer id) throws Exception {
- Items items = itemsMapper.selectByPrimaryKey(id);
- if(items == null){
- throw new CustomException("修改的商品不存在!");
- }
- //對於商品信息進行業務處理
- //....
- //最終返回ItemsCustom
- ItemsCustom itemsCustom = null;
- //將items的內容拷貝到itemsCustom
- if(items != null){
- itemsCustom = new ItemsCustom();
- BeanUtils.copyProperties(items,itemsCustom);
- }
- return itemsCustom;
- }
原博客:http://blog.csdn.net/fjnmbb12/article/details/73927539