SpringMVC統(tǒng)一異常處理三種方法詳解
這篇文章主要介紹了SpringMVC-統(tǒng)一異常處理三種方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
在 Spring MVC 應(yīng)用的開發(fā)中,不管是對底層數(shù)據(jù)庫操作,還是業(yè)務(wù)層或控制層操作,都會不可避免地遇到各種可預(yù)知的、不可預(yù)知的異常需要處理。
如果每個過程都單獨處理異常,那么系統(tǒng)的代碼耦合度高,工作量大且不好統(tǒng)一,以后維護(hù)的工作量也很大。
如果能將所有類型的異常處理從各層中解耦出來,這樣既保證了相關(guān)處理過程的功能單一,又實現(xiàn)了異常信息的統(tǒng)一處理和維護(hù)。
幸運的是,Spring MVC 框架支持這樣的實現(xiàn)。Spring MVC 統(tǒng)一異常處理有以下 3 種方式:
- 使用 Spring MVC 提供的簡單異常處理器 SimpleMappingExceptionResolver。
- 實現(xiàn) Spring 的異常處理接口 HandlerExceptionResolver 自定義自己的異常處理器。
- 使用 @ExceptionHandler 注解實現(xiàn)異常處理
本節(jié)主要根據(jù)這 3 種處理方式講解 Spring MVC 應(yīng)用的異常統(tǒng)一處理。
Spring MVC使用SimpleMappingExceptionResolver類異常處理
<?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 的對應(yīng)關(guān)系) -->
<bean
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!-- 定義默認(rèn)的異常處理頁面,當(dāng)該異常類型注冊時使用 -->
<property name="defaultErrorView" value="error"></property>
<!-- 定義異常處理頁面用來獲取異常信息的變量名,默認(rèn)名為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>
<!-- 在這里還可以繼續(xù)擴展對不同異常類型的處理 -->
</props>
</property>
</bean>
</beans>
Spring MVC使用HandlerExceptionResolver接口異常處理
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);
// 根據(jù)不同錯誤轉(zhuǎn)向不同頁面(統(tǒng)一處理),即異常與View的對應(yīng)關(guān)系
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);
}
}
}
<?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注解異常處理
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);
// 根據(jù)不同錯誤轉(zhuǎn)向不同頁面,即異常與view的對應(yīng)關(guān)系
if (ex instanceof SQLException) {
return "sql-error";
} else if (ex instanceof MyException) {
return "my-error";
} else {
return "error";
}
}
}
<?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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SQL Server2008 R2 數(shù)據(jù)庫鏡像實施手冊(雙機)SQL Server2014同樣適用
這篇文章主要介紹了SQL Server2008 R2 數(shù)據(jù)庫鏡像實施手冊(雙機)SQL Server2014同樣適用,需要的朋友可以參考下2017-04-04
SQL Server 2008登錄錯誤:無法連接到(local)解決方法
在一些朋友安裝完SQL Server 2008之后大多會遇到連接出錯的問題比如:SQL Server 2008登錄錯誤:無法連接到(local)等等相關(guān)問題,本文將詳細(xì)介紹解決方法,需要的朋友可以參考下2012-12-12
詳解SQL Server 2008工具SQL Server Profiler
這篇文章主要介紹了詳解SQL Server 2008工具SQL Server Profiler,本文逐一講解了SQL Server Profiler提供的功能,需要的朋友可以參考下2015-05-05
SQL Server 2008怎樣添加自增列實現(xiàn)自增序號
有的表需要添加自增列,在添加新紀(jì)錄時自動添加一個序號,有兩種不錯的方法通過T-SQL代碼、通過企業(yè)管理器在此分享給大家2013-09-09
如何在SQL Server 2008下輕松調(diào)試T-SQL語句和存儲過程
sqlserver2008調(diào)試的要求和條件:如果在引擎所在的電腦或服務(wù)器上調(diào)試,則只需要SA或者WINDOWS用戶登陸即可。如果是異地調(diào)試,則需要設(shè)置防火墻例外,增加SSMS和SQLSERVER.EXE為允許,增加135端口允許通過2013-10-10
SQL Server 2008 R2 超詳細(xì)安裝圖文教程
這篇文章主要介紹了SQL Server 2008 R2 超詳細(xì)安裝圖文教程,需要的朋友可以參考下2015-09-09
SQL Server 2008中SQL之WaitFor使用介紹
在SQL Server 2005以上版本中,在一個增強的WaitFor命令,其作用可以和一個job相當(dāng)。但使用更加簡捷2011-05-05
如何把Excel數(shù)據(jù)導(dǎo)入到SQL2008數(shù)據(jù)庫的實例方法
最近想練習(xí)一下批量插入數(shù)據(jù),所以從網(wǎng)上找了一下資料,做了一個怎么把Excel文件數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫。2013-04-04
SQL Server數(shù)據(jù)庫管理員(DBA)的工作內(nèi)容
DBA的工作目標(biāo)就是確保Microsoft SQL Server 2008系統(tǒng)正常高效地運行。DBA的工作也是最繁忙的工作,無論是性能調(diào)整,還是災(zāi)難恢復(fù),都離不開DBA的支持2013-10-10

