8. SpringMVC implements custom interceptors

8. SpringMVC implements custom interceptors

1 The role of interceptors

Spring MVC's interceptor is similar to the filter in Servlet development, which is used to pre-process and post-process the processor.

The interceptors are linked into a chain in a certain order, which is called the InterceptorChain. When an intercepted method or field is accessed
, the interceptors in the interceptor chain are called in the order in which they were previously defined. Interceptor is also a concrete realization of AOP idea
.

2 The difference between interceptors and filters

Regarding the difference between interceptor and filter, as shown in the figure:

image-20220312175950052

3. Implementation process

Step analysis:

  1. Create an interceptor class to implement the HandlerInterceptor interface
  2. Configure the interceptor
  3. Test the interception effect of the interceptor

3.1 Create an interceptor class to implement the HandlerInterceptor interface

public class MyInterceptor implements HandlerInterceptor {
    
    

    /**
     *         preHandle: 在目标方法执行之前 进行拦截   return false:不放行
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    
    
        System.out.println("preHandle1....");

        return true;
    }

    /**
     *       postHandle: 在目标方法执行之后,视图对象返回之前,执行的方法
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    
    
        System.out.println("postHandle1....");
    }

    /**
     *        afterCompletion:在流程都执行完成后,执行的方法
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    
    
        System.out.println("afterCompletion1....");
    }


}

3.2 Configure the interceptor

<!--    配置拦截器-->
    <mvc:interceptors>
        <mvc:interceptor>
            <!--对哪些资源执行拦截操作 path="/**"表示对controller层所有方法进行拦截 -->
            <mvc:mapping path="/**" />
            <bean class="com.weihong.interceptor.MyInterceptor" />
        </mvc:interceptor>
    </mvc:interceptors>

3.3 Test the interception effect of the interceptor

    @RequestMapping("/target")
    public String targetMethed(){
    
    
        System.out.println("目标方法执行了");
        return "interceptor";
    }

3.4 Writing jsp pages

<html>
<head>
    <title>Title</title>
</head>
<body>
    <h3>success~~~~~~</h3>
    <% System.out.println("视图执行了....");%>
</body>
</html>

3.5 Test Results

image-20220312181744982

4. Interceptor chain

In development, interceptors can be used alone, or multiple interceptors can be used at the same time to form an interceptor chain. The development steps are the same as a single interceptor, except that multiple registrations are registered. Note that the order of registration here represents the order in which the interceptors are executed.

Same as above, write another MyHandlerInterceptor2 operation to test the execution order:

<!--配置拦截器-->
<mvc:interceptors>
  <mvc:interceptor>
    <!--拦截器路径配置-->
    <mvc:mapping path="/**"/>
    <!--自定义拦截器类-->
    <bean class="com.lagou.interceptor.MyInterceptor1"></bean>
  </mvc:interceptor>
  <mvc:interceptor>
    <!--拦截器路径配置-->
    <mvc:mapping path="/**"/>
    <!--自定义拦截器类-->
    <bean class="com.lagou.interceptor.MyInterceptor2"></bean>
  </mvc:interceptor>
</mvc:interceptors>

5. Knowledge summary

The methods in the interceptor are described as follows:

image-20220312181936076

Guess you like

Origin blog.csdn.net/qq_41239465/article/details/123493219