淺談springMVC攔截器和過(guò)濾器總結(jié)
攔截器: 用來(lái)對(duì)訪問(wèn)的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 可以用來(lái)修改信息,跳轉(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 {
}
}
過(guò)濾器: 依賴于servlet容器,使用回調(diào)函數(shù),過(guò)濾范圍大
攔截器: 依賴于框架容器 比如spring、mybatis ,靈活
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot分頁(yè)的實(shí)現(xiàn)與long型id精度丟失問(wèn)題的解決方案介紹
在以后的開發(fā)中,當(dāng)全局唯一id的生成策略生成很長(zhǎng)的Long型數(shù)值id之后會(huì)超過(guò)JS對(duì)Long型數(shù)據(jù)處理的能力范圍,可能發(fā)生精度丟失而造成后端方法失效,我們要學(xué)會(huì)解決。分頁(yè)功能雖然簡(jiǎn)單但是非常重要,對(duì)于剛接觸項(xiàng)目的人一定要重點(diǎn)注意2022-10-10
spring注解如何為bean指定InitMethod和DestroyMethod
這篇文章主要介紹了spring注解如何為bean指定InitMethod和DestroyMethod,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java隨機(jī)生成手機(jī)短信驗(yàn)證碼的方法
這篇文章主要介紹了Java隨機(jī)生成手機(jī)短信驗(yàn)證碼的方法,涉及Java數(shù)學(xué)運(yùn)算計(jì)算隨機(jī)數(shù)及字符串操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Java多線程讀寫鎖ReentrantReadWriteLock類詳解
本文詳細(xì)講解了Java多線程讀寫鎖ReentrantReadWriteLock類,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12
通過(guò)AOP環(huán)繞通知如何實(shí)現(xiàn)事務(wù)控制
這篇文章主要介紹了通過(guò)AOP環(huán)繞通知如何實(shí)現(xiàn)事務(wù)控制的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
基于HTTP協(xié)議實(shí)現(xiàn)簡(jiǎn)單RPC框架的方法詳解
RPC全名(Remote?Procedure?Call),翻譯過(guò)來(lái)就是遠(yuǎn)程過(guò)程調(diào)用,本文將為大家介紹如何基于HTTP協(xié)議實(shí)現(xiàn)簡(jiǎn)單RPC框架,感興趣的小伙伴可以了解一下2023-06-06
詳解Java的MyBatis框架與Spring框架整合中的映射器注入
映射器注入方式可以將MyBatis與Spring映射好的XML文件實(shí)現(xiàn)配置共用,這里我們就來(lái)詳解Java的MyBatis框架與Spring框架整合中的映射器注入:2016-06-06
spring boot2.0實(shí)現(xiàn)優(yōu)雅停機(jī)的方法
這篇文章主要介紹了spring boot2.0實(shí)現(xiàn)優(yōu)雅停機(jī)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05

