SSM-SpringMVC interceptor detailed introduction

Introduction to SpringMVC interceptor

(1 Introduction

(1) interceptor, 是springmvc提供的用来拦截请求的
(2) Filter similar to Servlet.
(3) Add certain processing to certain requests.
(4) In springMVC, all requests will go to the front controller. The interceptor cooperates with the front-end control to process certain requests.
(5) It is often used for permission verification, logging of requested information, and judging whether a user is logged in, etc.
(6) SpringMVC's interceptor is implemented based on Spring AOP.

(2) The difference between interceptor and filter

filter, which is provided by javaEE to intercept requests, all requests can be intercepted, the
latter can only intercept the method of the controller
Insert picture description here

(3) Three methods of interceptor

HandlerInterceptor interface
execution order

Single interceptor

(1) Implement the HandlerInterceptor interface

//1:实现一个接口HandlerInterceptor
public class Demo01Interceptor  implements HandlerInterceptor {
    
    
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    
    
        System.out.println("preHandle");
        return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    
    

    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    
    

    }
}

(2) Rewrite preHandle, postHandle, afterCompletion methods

Insert picture description here

  • preHandle is executed
    before the handler method processes the request. This method returns boolean type data, which means that if it returns true, continue to execute the following method, if it returns false, the latter method will not be executed.
    The latter methods refer to: (1) the methods of other interceptors (2) the methods of this interceptor. (3) The requested handler method will not be executed.
    Summary: (1) If the interceptor's preHandle method returns false, then the other interceptor methods (preHandle, postHandle and afterCompletion) will not be executed.
    (2) If the preHandle method of the interceptor returns false, the other methods (postHandle and afterCompletion) of the interceptor will not be executed. For example: request /offer/saveOffer, the code in the prHandle method will be executed before the saveOffer method is executed.
    (3) The method in the Controller will not be executed.

  • postHandle is executed
    after the handler method processes the request and before returning to ModelAndView.

  • afterCompletion is
    executed after the request is completed.

(3) Configure the interceptor

Configured in the springMVC configuration file.

  • (1) Global interceptor configuration
    means to intercept all requests.
<mvc:interceptors>
  		<!-- 配置一个全局拦截器,拦截所有请求 -->
  		<bean class="interceptor.TestInterceptor"/>
</mvc:interceptors>
  • (2) Specific path interceptor

* Configure the interception path
* Configure the interceptor class to be executed.

<!-- 配置拦截器 -->
  	<mvc:interceptors>
  		<mvc:interceptor>
  			<!-- 配置拦截器作用的路径 -->
  			<mvc:mapping path="/**"/>
  			<!-- 配置不需要拦截作用的路径 -->
  			<mvc:exclude-mapping path=""/>
  			<!-- 定义在<mvc:interceptor>元素中,表示匹配指定路径的请求才进行拦截 -->
  			<bean class="interceptor.Interceptor1"/>
  		</mvc:interceptor>
  		<mvc:interceptor>
  			<!-- 配置拦截器作用的路径 -->
  			<mvc:mapping path="/gotoTest"/>
  			<!-- 定义在<mvc:interceptor>元素中,表示匹配指定路径的请求才进行拦截 -->
  			<bean class="interceptor.Interceptor2"/>
  		</mvc:interceptor>
  	</mvc:interceptors>

Multiple interceptors

(1) What is a multi-interceptor

Application software can be configured with many interceptors, and different interceptors implement different functions.

(2) Develop multiple interceptors

Same as the single interceptor, develop multiple interceptors with different functions. Different functions actually refer to the different implementation logic of the interceptor for the three methods of HandlerInceptor.

(3) Configure multiple interceptors

  • Configure global multi-interceptors
    Configure multiple Beans in mvc:interceptors.
  • Configure multiple interceptors for a certain path
    . Configure multiple mvc:interceptors in mvc:interceptors.
  • When configuring multiple springMVC interceptors, there is a sequence. In order from top to bottom.

springmvc.xml

<!--拦截器使用第二步:配置拦截器-->
    <mvc:interceptors>
        <!--用于测试的拦截器-->
        <mvc:interceptor>
            <!--拦截路径的配置-->
            <mvc:mapping path="/demo05.action"/>
            <bean id="interceptor1" class="com.wzx.interceptor.Demo01Interceptor"/>
        </mvc:interceptor>
        <mvc:interceptor>
            <!--拦截路径的配置-->
            <mvc:mapping path="/demo05.action"/>
            <bean id="interceptor2" class="com.wzx.interceptor.Demo02Interceptor"/>
        </mvc:interceptor>
        <mvc:interceptor>
            <!--拦截路径的配置-->
            <mvc:mapping path="/demo05.action"/>
            <bean id="interceptor3" class="com.wzx.interceptor.Demo03Interceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>

(4) The execution process of multiple interceptors

Insert picture description here
to sum up:

  • (1) The order of interceptors is the same as the order of interceptors configured in springMVC.
  • (2) Execute the preHandle method of the first interceptor first. If preHandle1 returns true. Execute preHanle2 of interceptor 2. After all the PreHandle methods are executed and they are all true, the corresponding method of the Controller will be executed.
  • (3) After the Controller method is executed, execute the posthandle method of the subsequent interceptor first. As shown in the figure above, execute postHandle2 first, and then execute postHandle1.
  • (4) After executing the postHandle methods of all interceptors, the controller returns to ModelAndView.
  • (5) After the Controller processes the request, it executes the afterCompletion method of the subsequent interceptor. As shown in the figure above, execute afterCompletion2 first, and then execute afterCompletion1.

Guess you like

Origin blog.csdn.net/qq_41209886/article/details/109109941