淺談springMVC攔截器和過濾器總結(jié)
攔截器: 用來對(duì)訪問的url進(jìn)行攔截處理
用處: 權(quán)限驗(yàn)證,亂碼設(shè)置等
spring-mvc.xml文件中的配置:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/tx ">
<!--編寫攔截器--> <mvc:interceptors> <!--對(duì)特定的url攔截--> <mvc:interceptor> <mvc:mapping path="/test.do"/> <bean class="com.hbut.interceptor.TestInterceptor"/> </mvc:interceptor> <mvc:interceptor> <!--對(duì)特定的模塊攔截第一級(jí)別攔截--> <mvc:mapping path="/test/×/"/> <bean class="com.hbut.interceptor.TestInterceptor1"/> </mvc:interceptor> <mvc:interceptor> <!--對(duì)特定的模塊攔截--> <mvc:mapping path="/test/×"/> <bean class="com.hbut.interceptor.TestInterceptor2"/> </mvc:interceptor> </mvc:interceptors>
對(duì)所有的url進(jìn)行攔截
<mvc:interceptors> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/> </mvc:interceptors>
java代碼
package com.hbut.interceptor; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @Author XiJun.Gong * @DATE 2016/6/1. * aim: com.hbut.interceptor */ public class TestInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { //todo 在此處添加要操作code System.out.println("preHandle"); return true; //todo 此處為false時(shí),請(qǐng)求不會(huì)到達(dá)control層 } @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { System.out.println("postHandle"); //todo 可以用來修改信息,跳轉(zhuǎn)等 } @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { System.out.println("afterCompletion"); //todo 最后執(zhí)行 } }
另一種攔截器:大同小異
package com.hbut.interceptor; import org.springframework.ui.ModelMap; import org.springframework.web.context.request.WebRequest; import org.springframework.web.context.request.WebRequestInterceptor; /** * @Author XiJun.Gong * @DATE 2016/6/1. * aim: com.hbut.interceptor */ public class Test2Interceptor implements WebRequestInterceptor { @Override public void preHandle(WebRequest webRequest) throws Exception { } @Override public void postHandle(WebRequest webRequest, ModelMap modelMap) throws Exception { } @Override public void afterCompletion(WebRequest webRequest, Exception e) throws Exception { } }
過濾器: 依賴于servlet容器,使用回調(diào)函數(shù),過濾范圍大
攔截器: 依賴于框架容器 比如spring、mybatis ,靈活
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java中l(wèi)ist集合的clear方法及空字符串的區(qū)別
這篇文章主要介紹了Java中l(wèi)ist集合的clear方法及空字符串的區(qū)別,在使用list?結(jié)合的時(shí)候習(xí)慣了?list=null?;在創(chuàng)建這樣的方式,但是發(fā)現(xiàn)使用list的clear?方法很不錯(cuò),尤其是有大量循環(huán)的時(shí)候<BR>list.clear()與list?=?null?區(qū)別,需要的朋友可以參考下2023-08-08使用開源項(xiàng)目JAVAE2 進(jìn)行視頻格式轉(zhuǎn)換
這篇文章主要介紹了使用開源項(xiàng)目JAVAE 進(jìn)行視頻格式轉(zhuǎn)換,幫助大家更好的利用Java處理視頻,完成自身需求,感興趣的朋友可以了解下2020-11-11SpringCloud?Stream?整合RabbitMQ的基本步驟
這篇文章主要介紹了SpringCloud?Stream?整合RabbitMQ的基本步驟,從項(xiàng)目介紹到生產(chǎn)者結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03Spring Boot 簡(jiǎn)單使用EhCache緩存框架的方法
本篇文章主要介紹了Spring Boot 簡(jiǎn)單使用EhCache緩存框架的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07spring?@value無(wú)法取值多個(gè)properties文件的解決
這篇文章主要介紹了spring?@value無(wú)法取值多個(gè)properties文件的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03關(guān)于Spring源碼是如何解決Bean的循環(huán)依賴
這篇文章主要介紹了關(guān)于Spring源碼是如何解決Bean的循環(huán)依賴,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Springboot項(xiàng)目刪除項(xiàng)目同步target文件問題解決方案
這篇文章主要介紹了Springboot項(xiàng)目刪除項(xiàng)目同步target文件問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12