SpringMVC Basics--Processor Interceptor

The processor interceptor of Spring MVC is similar to the filter Filter in Servlet development, which is used to pre-process and post-process the processor.
When it comes to interceptors, I would like to mention a word to everyone - Interceptor Chain (Interceptor Chain). The interceptor chain is to connect the interceptors into a chain in a certain order. When accessing the intercepted method or field, the interceptors in the interceptor chain will be called in the order they were defined before.
Interceptors are somewhat similar to filters, let's compare them:

  • Filters are part of the servlet specification and can be used by any java web project.
  • The interceptor is the SpringMVC framework itself, and only projects that use the SpringMVC framework can use it.
  • After the filter is configured with /* in url-pattern, it can intercept all resources to be accessed, including static resources.
  • Interceptor is a controller method that only intercepts access. If the access is jsp, html, css, image or js, it will not be intercepted.

It is also a concrete application of AOP thought. If we want to customize the interceptor, it is required to implement: HandlerInterceptor interface.
Workflow diagram of the interceptor
insert image description here

Implementation of the interceptor

Step 1: Write an interceptor and implement the HandlerInterceptor interface

/*
自定义拦截器
* jdk1.8之后对接口进行了增强,可以在接口中实现方法,所以我们实现接口没有实现方法时并没有报错
* */
public class MyInterceptor implements HandlerInterceptor {
    
    

    // 预处理
    // 返回值为true: 方行->执行下一个拦截器(没有跳过)->controller方法
    // 返回值为false: 不放行
    // 在预处理中做逻辑判断等
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    
    
        System.out.println("拦截器执行了--预处理111");
        return true;
    }

    // 后处理,执行顺序和上面一样
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    
    
        System.out.println("拦截器执行了--后处理111");
    }

    // 最后处理,在浏览器跳转到指定页面之后再执行
    // 一般用于释放资源
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    
    
        System.out.println("拦截器执行了--最后处理111");
    }
}

Step 2: Configure the interceptor

<!-- 配置拦截器 -->
    <mvc:interceptors>
        <!-- 配置第一个拦截器 -->
        <mvc:interceptor>
            <!-- 要拦截的具体方法 -->
            <mvc:mapping path="/user/*"/>
            <!-- 不要拦截的方法,和上者设置一个即可 -->
          <!--  <mvc:exclude-mapping path=""/>-->
            <!-- 配置该拦截对象 -->
            <bean id="myInterceptor1" class="cn.Interceptor.MyInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>

Step Three: Test
insert image description here

interceptor chain

That is to define multiple interceptors, the execution order of multiple interceptors is as follows
insert image description here
On the basis of the above, define an interceptor

public class MyInterceptor2 implements HandlerInterceptor {
    
    

    // 预处理
    // 返回值为true: 方行->执行下一个拦截器(没有跳过)->controller方法
    // 返回值为false: 不放行
    // 在预处理中做逻辑判断等
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    
    
        System.out.println("拦截器执行了--预处理222");
        return true;
    }

    // 后处理,执行顺序和上面一样
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    
    
        System.out.println("拦截器执行了--后处理222");
    }

    // 最后处理,在浏览器跳转到指定页面之后再执行
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    
    
        System.out.println("拦截器执行了--最后处理222");
    }
}

Configure one more interceptor in the configuration file

    <!-- 配置拦截器 -->
    <mvc:interceptors>
        <!-- 配置第一个拦截器 -->
        <mvc:interceptor>
            <!-- 要拦截的具体方法 -->
            <mvc:mapping path="/user/*"/>
            <!-- 不要拦截的方法,和上者设置一个即可 -->
          <!--  <mvc:exclude-mapping path=""/>-->
            <!-- 配置该拦截对象 -->
            <bean id="myInterceptor1" class="cn.Interceptor.MyInterceptor"></bean>
        </mvc:interceptor>
        <!-- 配置第二个拦截器 -->
        <mvc:interceptor>
            <!-- 要拦截的具体方法 -->
            <mvc:mapping path="/user/*"/>
            <!-- 不要拦截的方法,和上者设置一个即可 -->
            <!--  <mvc:exclude-mapping path=""/>-->
            <!-- 配置该拦截对象 -->
            <bean id="myInterceptor2" class="cn.Interceptor.MyInterceptor2"></bean>
        </mvc:interceptor>
    </mvc:interceptors>

test
insert image description here

Guess you like

Origin blog.csdn.net/qq_44660367/article/details/108949440