一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|數據庫技術|

服務器之家 - 數據庫 - Sql Server - SpringMVC統一異常處理三種方法詳解

SpringMVC統一異常處理三種方法詳解

2020-06-24 15:45流氓大隊長 Sql Server

這篇文章主要介紹了SpringMVC-統一異常處理三種方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

這篇文章主要介紹了SpringMVC-統一異常處理三種方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

在 Spring MVC 應用的開發中,不管是對底層數據庫操作,還是業務層或控制層操作,都會不可避免地遇到各種可預知的、不可預知的異常需要處理。

如果每個過程都單獨處理異常,那么系統的代碼耦合度高,工作量大且不好統一,以后維護的工作量也很大。

如果能將所有類型的異常處理從各層中解耦出來,這樣既保證了相關處理過程的功能單一,又實現了異常信息的統一處理和維護。

幸運的是,Spring MVC 框架支持這樣的實現。Spring MVC 統一異常處理有以下 3 種方式:

  • 使用 Spring MVC 提供的簡單異常處理器 SimpleMappingExceptionResolver。
  • 實現 Spring 的異常處理接口 HandlerExceptionResolver 自定義自己的異常處理器。
  • 使用 @ExceptionHandler 注解實現異常處理

本節主要根據這 3 種處理方式講解 Spring MVC 應用的異常統一處理。

Spring MVC使用SimpleMappingExceptionResolver類異常處理

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring一beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">
  <!-- 使用掃描機制掃描包 -->
  <context:component-scan base-package="controller" />
  <context:component-scan base-package="service" />
  <context:component-scan base-package="dao" />
  <!-- 配置視圖解析器 -->
  <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    id="internalResourceViewResolver">
    <!--前綴 -->
    <property name="prefix" value="/WEB-INF/jsp/" />
    <!-- 后綴 -->
    <property name="suffix" value=".jsp" />
  </bean>
  <!--SimpleMappingExceptionResolver(異常類與 View 的對應關系) -->
  <bean
    class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <!-- 定義默認的異常處理頁面,當該異常類型注冊時使用 -->
    <property name="defaultErrorView" value="error"></property>
    <!-- 定義異常處理頁面用來獲取異常信息的變量名,默認名為exception -->
    <property name="exceptionAttribute" value="ex"></property>
    <!-- 定義需要特殊處理的異常,用類名或完全路徑名作為key,異常頁名作為值 -->
    <property name="exceptionMappings">
      <props>
        <prop key="exception.MyException">my-error</prop>
        <prop key="java.sql.SQLException">sql-error</prop>
        <!-- 在這里還可以繼續擴展對不同異常類型的處理 -->
      </props>
    </property>
  </bean>
</beans>

Spring MVC使用HandlerExceptionResolver接口異常處理

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package exception;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
public class MyExceptionHandler implements HandlerExceptionResolver {
  @Override
  public ModelAndView resolveException(HttpServletRequest arg0,
      HttpServletResponse arg1, Object arg2, Exception arg3) {
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("ex", arg3);
    // 根據不同錯誤轉向不同頁面(統一處理),即異常與View的對應關系
    if (arg3 instanceof MyException) {
      return new ModelAndView("my-error", model);
    } else if (arg3 instanceof SQLException) {
      return new ModelAndView("sql-error", model);
    } else {
      return new ModelAndView("error", model);
    }
  }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring一beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">
  <!-- 使用掃描機制掃描包 -->
  <context:component-scan base-package="controller" />
  <context:component-scan base-package="service" />
  <context:component-scan base-package="dao" />
  <!-- 配置視圖解析器 -->
  <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    id="internalResourceViewResolver">
    <!--前綴 -->
    <property name="prefix" value="/WEB-INF/jsp/" />
    <!-- 后綴 -->
    <property name="suffix" value=".jsp" />
  </bean>
  <!--托管MyExceptionHandler-->
  <bean class="exception.MyExceptionHandler"/>
</beans>

Spring MVC使用@ExceptionHandler注解異常處理

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package controller;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.ExceptionHandler;
import exception.MyException;
public class BaseController {
  /** 基于@ExceptionHandler異常處理 */
  @ExceptionHandler
  public String exception(HttpServletRequest request, Exception ex) {
    request.setAttribute("ex", ex);
    // 根據不同錯誤轉向不同頁面,即異常與view的對應關系
    if (ex instanceof SQLException) {
      return "sql-error";
    } else if (ex instanceof MyException) {
      return "my-error";
    } else {
      return "error";
    }
  }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring一beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">
  <!-- 使用掃描機制掃描包 -->
  <context:component-scan base-package="controller" />
  <context:component-scan base-package="service" />
  <context:component-scan base-package="dao" />
  <!-- 配置視圖解析器 -->
  <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    id="internalResourceViewResolver">
    <!--前綴 -->
    <property name="prefix" value="/WEB-INF/jsp/" />
    <!-- 后綴 -->
    <property name="suffix" value=".jsp" />
  </bean>
</beans>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.cnblogs.com/lowerma/p/11836595.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 波多野结衣在线中文字幕 | 国产新疆成人a一片在线观看 | 继的朋友无遮漫画免费观看73 | 欧美日韩国产成人精品 | 星球大战成人h无删减版 | 国产精品猎奇系列在线观看 | 久久五月综合婷婷中文云霸高清 | sxx免费看视频在线播放 | 99热这里只有精品在线 | 农村妇女野战bbxxx | 国产精品久久久久aaaa | 三级午夜宅宅伦不卡在线 | 99精品全国免费7观看视频 | 石原莉奈adn093店长未婚妻 | 日本男男gaygays | 亚洲精品www久久久久久久软件 | 美女班主任让我爽了一夜视频 | meyd–456佐山爱在线播放 | 99视频九九精品视频在线观看 | 日韩天堂在线 | 91夜夜操 | 亚洲精品久久碰 | 午夜性色一区二区三区不卡视频 | 窝窝午夜精品一区二区 | 国产成人影院 | 午夜伦伦电影理论片大片 | 18无删减羞羞网站动漫 | 91丝袜足控免费网站xx | 好大好硬好深好爽想要之黄蓉 | 1024国产基地永久免费 | 日处女b| 美女脱了内裤打开腿让人羞羞软件 | 四虎影音| 无人区乱码1区2区3区网站 | 亚洲国产精品嫩草影院久久 | 236宅宅2021最新理论 | 国产亚洲人成网站在线观看不卡 | 日本老妇和子乱视频 | 情趣内衣情趣玩具play | 性做久久久久久久久老女人 | 男人桶女下面60分钟视频 |