欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringMVC Interceptor攔截器使用教程

 更新時(shí)間:2023年01月14日 10:21:51   作者:qq_19286785  
SpringMVC中攔截器(Interceptor)用于對URL請求進(jìn)行前置/后置過濾,Interceptor與Filter用途相似,但實(shí)現(xiàn)方式不同。Interceptor底層就是基于Spring AOP 面向切面編程實(shí)現(xiàn)

SpringMVC中的攔截器用于攔截控制器方法的執(zhí)行,執(zhí)行在Controller前后,和視圖渲染完成后。如下圖所示:

一、創(chuàng)建攔截器

繼承HandlerInterceptor 接口,并實(shí)現(xiàn)其中的方法

public class FirstInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("FirstInterceptor===>preHandle");
        return true;
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("FirstInterceptor===>postHandle");
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("FirstInterceptor===>afterCompletion");
    }
}

SpringMVC中的攔截器有三個抽象方法:

preHandle:控制器方法執(zhí)行之前執(zhí)行preHandle(),其boolean類型的返回值表示是否攔截或放行,返回true為放行,即調(diào)用控制器方法;返回false表示攔截,即不調(diào)用控制器方法 postHandle:控制器方法執(zhí)行之后執(zhí)行postHandle() afterComplation:處理完視圖和模型數(shù)據(jù),渲染視圖完畢之后執(zhí)行afterComplation()

二、Spring配置文件中設(shè)置

    <mvc:interceptors>
        <bean class="com.rzg.interceptor.FirstInterceptor"></bean>
    </mvc:interceptors>

上面的配置等價(jià)與下面

    <bean id="firstInterceptor" class="com.rzg.interceptor.FirstInterceptor"></bean>
    <mvc:interceptors>
        <ref bean="firstInterceptor"/>
    </mvc:interceptors>

如果只在mvc:interceptors中配置一個攔截器,那么所有的請求都經(jīng)過攔截器。以上配置的請求結(jié)果如下:

FirstInterceptor===>preHandle
FirstInterceptor===>postHandle
FirstInterceptor===>afterCompletion

如果要對部分控制器進(jìn)行攔截,可以設(shè)置設(shè)置interceptor。以下配置將攔截所有的/*請求,例如/hello 、/abc ,但是不攔截/abc/cde。如果要攔截任意請求,使用/**

<bean id="firstInterceptor" class="com.rzg.interceptor.FirstInterceptor"></bean>
<!--    攔截器的設(shè)置-->
    <mvc:interceptors>
       <mvc:interceptor>
       <!--攔截的請求   -->
           <mvc:mapping path="/*"/>   
           <!-- 在mapping中排除以下攔截路徑-->
           <mvc:exclude-mapping path="/hello2"/>
           <ref bean="firstInterceptor"/>
       </mvc:interceptor>
    </mvc:interceptors>

結(jié)果:

請求/hello FirstInterceptor===>preHandle
FirstInterceptor===>postHandle
FirstInterceptor===>afterCompletion
請求/hello2 不經(jīng)過攔截器

三、設(shè)置多個攔截器

兩個攔截器FirstInterceptor、SecondInterceptor。

配置文件中配置:

    <mvc:interceptors>
       <mvc:interceptor>
           <mvc:mapping path="/**"/>
           <bean class="com.rzg.interceptor.FirstInterceptor"/>
       </mvc:interceptor>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="com.rzg.interceptor.SecondInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>

請求結(jié)果:

FirstInterceptor===>preHandle
SecondInterceptor===>preHandle
SecondInterceptor===>postHandle
FirstInterceptor===>postHandle
SecondInterceptor===>afterCompletion
FirstInterceptor===>afterCompletion

若每個攔截器的preHandle()都返回true

此時(shí)多個攔截器的執(zhí)行順序和攔截器在SpringMVC的配置文件的配置順序有關(guān):

preHandle()會按照配置的順序執(zhí)行,而postHandle()和afterComplation()會按照配置的反序執(zhí)行

若某個攔截器的preHandle()返回了false

preHandle()返回false和它之前的攔截器的preHandle()都會執(zhí)行,postHandle()都不執(zhí)行,返回false的攔截器之前的攔截器的afterComplation()會執(zhí)行

將SecondInterceptor 攔截器 preHandle 中返回 false

public class SecondInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, 	Object handler) throws Exception {
        System.out.println("SecondInterceptor===>preHandle");
    	return false;
    }

測試結(jié)果:

FirstInterceptor===>preHandle
SecondInterceptor===>preHandle
FirstInterceptor===>afterCompletio

到此這篇關(guān)于SpringMVC Interceptor攔截器使用教程的文章就介紹到這了,更多相關(guān)SpringMVC Interceptor內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論