這篇文章主要介紹了SpringMVC-統(tǒng)一異常處理三種方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
在 Spring MVC 應(yīng)用的開(kāi)發(fā)中,不管是對(duì)底層數(shù)據(jù)庫(kù)操作,還是業(yè)務(wù)層或控制層操作,都會(huì)不可避免地遇到各種可預(yù)知的、不可預(yù)知的異常需要處理。
如果每個(gè)過(guò)程都單獨(dú)處理異常,那么系統(tǒng)的代碼耦合度高,工作量大且不好統(tǒng)一,以后維護(hù)的工作量也很大。
如果能將所有類(lèi)型的異常處理從各層中解耦出來(lái),這樣既保證了相關(guān)處理過(guò)程的功能單一,又實(shí)現(xiàn)了異常信息的統(tǒng)一處理和維護(hù)。
幸運(yùn)的是,Spring MVC 框架支持這樣的實(shí)現(xiàn)。Spring MVC 統(tǒng)一異常處理有以下 3 種方式:
本節(jié)主要根據(jù)這 3 種處理方式講解 Spring MVC 應(yīng)用的異常統(tǒng)一處理。
Spring MVC使用SimpleMappingExceptionResolver類(lèi)異常處理
?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"> !-- 使用掃描機(jī)制掃描包 --> 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(異常類(lèi)與 View 的對(duì)應(yīng)關(guān)系) --> bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> !-- 定義默認(rèn)的異常處理頁(yè)面,當(dāng)該異常類(lèi)型注冊(cè)時(shí)使用 --> property name="defaultErrorView" value="error">/property> !-- 定義異常處理頁(yè)面用來(lái)獲取異常信息的變量名,默認(rèn)名為exception --> property name="exceptionAttribute" value="ex">/property> !-- 定義需要特殊處理的異常,用類(lèi)名或完全路徑名作為key,異常頁(yè)名作為值 --> property name="exceptionMappings"> props> prop key="exception.MyException">my-error/prop> prop key="java.sql.SQLException">sql-error/prop> !-- 在這里還可以繼續(xù)擴(kuò)展對(duì)不同異常類(lèi)型的處理 --> /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) { MapString, Object> model = new HashMapString, Object>(); model.put("ex", arg3); // 根據(jù)不同錯(cuò)誤轉(zhuǎn)向不同頁(yè)面(統(tǒng)一處理),即異常與View的對(duì)應(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"> !-- 使用掃描機(jī)制掃描包 --> 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ù)不同錯(cuò)誤轉(zhuǎn)向不同頁(yè)面,即異常與view的對(duì)應(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"> !-- 使用掃描機(jī)制掃描包 --> 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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
標(biāo)簽:平頂山 惠州 茂名 長(zhǎng)白山 仙桃 潛江 貴港 唐山
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《SpringMVC統(tǒng)一異常處理三種方法詳解》,本文關(guān)鍵詞 SpringMVC,統(tǒng)一,異常,處理,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。