Spring MVC assembly interceptors

When a request is received, DispatcherServlet the request to the handler mapping (HandlerMapping), you should find HandlerExecutionChain objects to the request. Before explaining HandlerMapping, it is necessary to know about this HandlerExecutionChain object.

HandlerExecutionChain name suggests is a chain of execution, the request comprising a processor (Handler) process, comprising a plurality of simultaneously intercept the request interceptor (HandlerInterceptor). When HandlerMapping return HandlerExecutionChain, DispatcherServlet defined in HandlerExecutionChain request to the processor and processed together interceptors.

HandlerExecutionChain is responsible for handling requests and return processing executing ModelAndView chain, the structure shown below. Before and after the request is executed Handler, the chain will be assembled Handlerlnterceptor embodiment interception.

Interceptor in the end to do what? We understand through several interface methods to examine the interceptor.
1) boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler): Before the request reaches Handler, to perform the pre-processing method. When this method returns false, the request is returned directly, it is not transmitted to the next interceptor in the chain, but not transmitted to the Handler chain ends in the processor. Only returns true, the request was passed to the next processing chain node.

2) void postHandle (HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView): after the request is executed HandlerAdapter, perform the post-processing method.

3) void afterCompletion (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex): After the response has been rendered, the method is performed.

The processor is located at a chain end Handler, for the DispatcherServlet Handler HandlerAdapter encapsulated by an adapter, the adapter interface according to uniform treatment Handler call.

<mvc:interceptors>
    <mvc:interceptor>
        <mapping path="/secure/*">
        <bean class="com.smart.web.MyInterceptor"/>
    </mvc:interceptor>
</mvc:interceptors>

Can be disposed in a plurality of smart-servlet.xml interceptors, each interceptor can specify a path map matching to limit the scope of the interceptor.

Guess you like

Origin www.cnblogs.com/jwen1994/p/11210901.html