12.SpringMVC of interceptor

1. interceptor Overview

1.1 What is a blocker?

Spring MVC The interceptor (Interceptor) similar to the filter (Filter) Servlet is, it is mainly for intercepting a user request and processed accordingly. For example permissions may be verified by the interceptor, the log recording request information, determines whether the user login and the like.
To use Spring MVC in the interceptor, we need to define and configure the interceptor class. Typically interceptor class can be defined in two ways.
1. HandlerInterceptor be defined by the implementation interface or inherit HandlerInterceptor interface implementation class (e.g. HandlerInterceptorAdapter).

2. WebRequestInterceptor implement an interface defined by, or inherited WebRequestInterceptor interface implementation class.

HandlerInterceptor interface mode in order to achieve, for example, a custom interceptor class code as follows:

public class CustomInterceptor implements HandlerInterceptor{
        public boolean preHandle(HttpServletRequest request, 
                                 HttpServletResponse response, Object handler)throws Exception {
            return false;
        }
        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 {
        }
    }

The above code, custom interceptor HandlerInterceptor implements interfaces, and implements three interface methods:

  • The preHandle () method: This method is performed before controller method, the interrupt return value indicates whether the subsequent operation. When it returns a value of true, indicating that it goes;
    when the return value is false, will break all the subsequent operations (including a method of performing the next call interceptor controller class and the like).

  • The postHandle () method: This method calls the method after the controller, and a view before analysis execution. This method can make further modifications to the model and view requests domain.

  • afterCompletion () method: This method throughout the request is complete, i.e., after the execution view rendering. You can achieve some resource cleanup, logs and other information to work through this method.

1.2 interceptor configuration

Development interceptor developed as servlet or filter, like, need to be configured in the configuration file, the configuration code is as follows:

    <! - Configuration interceptor -> 
    < MVC: interceptors > 
        <! - <the bean class = "com.ma.interceptor.CustomeInterceptor" /> -> 
        <! - blocker. 1 -> 
        < MVC: interceptor > 
            ! <- configuration interceptor role path -> 
            < MVC: Mapping path = "/ **" /> 
            < MVC:-Mapping the exclude path = "" /> 
            ! <- defined in <mvc: interceptor > request which matches the specified path to intercept only the following -> 
            < the bean class = "com.ma.interceptor.Intercptor1" /> 
        </ MVC:Interceptor > 
        <-! interceptor 2 ->
        <mvc:interceptor>
            <mvc:mapping path="/hello"/>
            <bean class="com.ma.interceptor.Interceptor2"/>
     </mvc:interceptor>

The above code, <mvc: interceptors> element is used to configure a set of interceptors, the base sub-element <bean> defined in a global interceptor, which intercepts all requests; and <mvc: interceptor> element definition is interceptor specified path, it requests under the specified path will take effect. <mvc: interceptor> child element <mvc: mapping> path configuration for the interceptor function, which is defined in its path in the path attributes. As described above path attribute value code "/ *" indicates block all paths, "/ hello" denotes block any "/ Hello" at the end of the path. If the content does not need to contain in the intercepted request path, can also <mvc: exclude-mapping> configuration element.
Note: sub-elements: <mvc interceptor> must be written in accordance with the order of arrangement in the above code, i.e. <mvc: mapping> <mvc: exclude-mapping> <bean>, otherwise the file error.

2. The execution flow interceptor

2.1 execution flow of a single interceptor

When running a program, executed interceptor there is a certain order, the order of the sequence associated with interceptors as defined profile.
Single interceptor, in the program execution process shown below:

1. The preHandle first implementation program () method returns true if the method, the program will continue execution in the processor-down, or down will not be executed.

2. After the service processor (i.e., the Controller class) request has been handled, executes The postHandle () method will then return a response to the client terminal through DispatcherServlet.

3. After the request has been handled DispatcherServlet will be performed afterCompletion () method.

More than 2.2 execution flow interceptor

A plurality of interceptors (assuming there are two interceptors Interceptor1 and Interceptor2, and in the configuration file, configuration Interceptor1 interceptor front), in the program execution flow as shown below:

As can be seen from Fig., When there are a plurality of interceptors simultaneously, they The preHandle () method will be performed in the order of arrangement of the profile in the interceptor, while they The postHandle () method and afterCompletion () method will be arranged in accordance with reverse order of the execution order.

Guess you like

Origin www.cnblogs.com/deityjian/p/11498867.html