springmvc中自定义拦截器以及拦截器的执行过程

幸运小侯子 2020-01-30

1.拦截器在一次请求中的执行流程

springmvc中自定义拦截器以及拦截器的执行过程

springmvc中自定义拦截器以及拦截器的执行过程


2.拦截器入门案例

2.1springMVC.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        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-4.0.xsd">

        <context:component-scan base-package="com.atguigu"></context:component-scan>
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/view/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
        
        <!--
        静态资源需要默认的servlet来处理
          -->
        <mvc:default-servlet-handler/>
        <mvc:annotation-driven></mvc:annotation-driven>
        
       
        
        <mvc:interceptors>
            <!--  默认拦截所有请求-->
            <bean class="com.atguigu.interceptor.FirstInterceptor"></bean>
            <bean class="com.atguigu.interceptor.SecondInterceptor"></bean>
            <!-- 此方式要求拦截器上必须加上@Component -->
        <!--     <ref bean="firstInterceptor"/> -->
        
        <!--  自定义拦截方式-->
        <!--
             <mvc:interceptor>
                <bean></bean>
                <mvc:mapping path=""/>用于登入验证,如果session中没有值,拦截。有值,不拦截
                <mvc:exclude-mapping path=""/>有些功能不需要拦截,例如跳转到登入页面
            
            </mvc:interceptor>
        -->
        </mvc:interceptors>
        
</beans>

2.2 拦截器的编写

FirstInterceptor

package com.atguigu.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
@Component
public class FirstInterceptor implements HandlerInterceptor {

    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {
        // TODO Auto-generated method stub
        System.out.println("First:afterCompletion");
        
    }

    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
            throws Exception {
        // TODO Auto-generated method stub
        System.out.println("First:postHandle");
    }

    public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("First:preHandle");
        return true;
    }

}

SecondInterceptor

package com.atguigu.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
@Component
public class SecondInterceptor implements HandlerInterceptor {

    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {
        // TODO Auto-generated method stub
        System.out.println(" Second:afterCompletion");
        
    }

    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
            throws Exception {
        // TODO Auto-generated method stub
        System.out.println(" Second:postHandle");
    }

    public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
        // TODO Auto-generated method stub
        System.out.println(" Second:preHandle");
        return true;
    }

}

2.3拦截器的执行顺序

First:preHandle
 Second:preHandle
 Second:postHandle
First:postHandle
 Second:afterCompletion
First:afterCompletion

/**
     * 当有多个拦截器时,
     * preHandle,按照拦截器数组的正向顺序执行
     * postHandle,按照拦截器数组的反向顺序执行
     * afterCompletion,按照拦截器数组的反向顺序执行
     * 
     * 当多个拦截器的preHandle有不同的值时,
     * 第一个返回false,第二个返回false,只有第一个拦截器的preHandle()会执行
     * 第一个返回true,第二个返回false,(全部)第一、二个拦截器的preHandle()、(全部)postHandle()都不会执行,
     * 而afterCompletion()有(返回false的拦截器之前的)第一个会执行
     * 第一个返回false,第二个返回true:只有第一个的perhandle()会执行
     * 
     * @return
     */

相关推荐