在 Spring MVC 應用的開發中,無論是對底層數據庫操做,仍是業務層或控制層操做,都會不可避免地遇到各類可預知的、不可預知的異常須要處理。
若是每一個過程都單獨處理異常,那麼系統的代碼耦合度高,工做量大且很差統一,之後維護的工做量也很大。
若是能將全部類型的異常處理從各層中解耦出來,這樣既保證了相關處理過程的功能單一,又實現了異常信息的統一處理和維護。
幸運的是,Spring MVC 框架支持這樣的實現。Spring MVC 統一異常處理有如下 3 種方式:java
本節主要根據這 3 種處理方式講解 Spring MVC 應用的異常統一處理。web
<?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>
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); } } }
<?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>
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"; } } }
<?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>