Springmvc異常處理器及攔截器實現代碼
更新時間:2020年10月09日 09:31:51 作者:一路繁花似錦繡前程
這篇文章主要介紹了Springmvc異常處理器及攔截器實現代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
一、異常處理器
1、實現HandlerExceptionResolver接口
package com.wuxi.exceptions; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class RequestExceptionResolver implements HandlerExceptionResolver { @Override public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) { ModelAndView mv = new ModelAndView(); mv.addObject("errorMsg", e.getMessage());//錯誤信息 mv.setViewName("error");//請求轉發(fā)的頁面 return mv; } }
2、springmvc的xml配置文件
<?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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--掃描組件--> <context:component-scan base-package="com.wuxi"></context:component-scan> <!--視圖解析器--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--參數類型裝換器--> <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="com.wuxi.utils.StringToDateConverter"></bean> </set> </property> </bean> <!--配置調度器不攔截靜態(tài)資源--> <mvc:resources mapping="/css/**" location="/css/"></mvc:resources> <mvc:resources mapping="/images/**" location="/images/"></mvc:resources> <mvc:resources mapping="/js/**" location="/js/"></mvc:resources> <!--配置文件解析器對象--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10485760"></property> </bean> <!--異常處理器--> <bean id="requestExceptionResolver" class="com.wuxi.exceptions.RequestExceptionResolver"></bean> <!--開啟springmvc框架注解的支持--> <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> </beans>
二、攔截器
1、實現HandlerInterceptor接口
package com.wuxi.interceptors; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ControllerInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("controller的方法執(zhí)行之前執(zhí)行"); return true;//true:放行;false:攔截 } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("controller的方法執(zhí)行之后執(zhí)行"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("jsp執(zhí)行之后執(zhí)行"); } }
2、springmvc的xml配置文件
<?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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--掃描組件--> <context:component-scan base-package="com.wuxi"></context:component-scan> <!--視圖解析器--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--參數類型裝換器--> <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="com.wuxi.utils.StringToDateConverter"></bean> </set> </property> </bean> <!--配置調度器不攔截靜態(tài)資源--> <mvc:resources mapping="/css/**" location="/css/"></mvc:resources> <mvc:resources mapping="/images/**" location="/images/"></mvc:resources> <mvc:resources mapping="/js/**" location="/js/"></mvc:resources> <!--配置文件解析器對象--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10485760"></property> </bean> <!--異常處理器--> <bean id="requestExceptionResolver" class="com.wuxi.exceptions.RequestExceptionResolver"></bean> <!--攔截器--> <mvc:interceptors> <!--可配置多個攔截器,執(zhí)行順序pre1->pre2->controller->post2->post1->jsp->after2->after1--> <mvc:interceptor> <!--攔截的資源路徑--> <mvc:mapping path="/**"/> <!--不攔截的資源路徑--> <!--<mvc:exclude-mapping path="/hello"/>--> <bean class="com.wuxi.interceptors.ControllerInterceptor"></bean> </mvc:interceptor> </mvc:interceptors> <!--開啟springmvc框架注解的支持--> <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> </beans>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
SpringCloud協(xié)同開發(fā)實現方法淺析
好幾個人同時開發(fā)同一個服務上的不同模塊,導致你需要調試的接口總是被路由到別人的服務上,非常影響調試的效率,而且人越多越難受,總是請求不到自己的服務,這篇文章主要介紹了SpringCloud協(xié)同開發(fā)實現方法2022-12-12idea2020.1最新版永久破解/pycharm也可用(步驟詳解)
這篇文章主要介紹了idea2020.1最新版永久破解/pycharm也可用,本文給大家分享簡單實現步驟,通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04IDEA中的Run/Debug Configurations各項解讀
這篇文章主要介紹了IDEA中的Run/Debug Configurations各項解讀,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09基于Tomcat7、Java、WebSocket的服務器推送聊天室實例
HTML5 WebSocket實現了服務器與瀏覽器的雙向通訊,本篇文章主要介紹了基于Tomcat7、Java、WebSocket的服務器推送聊天室實例,具有一定的參考價值,有興趣的可以了解一下。2016-12-12