Interceptor release
The meaning of release is that if there is the next interceptor, the next one is executed, and if the interceptor is the last one in the interceptor chain, the method in the controller is executed.
Methods in the interceptor
preHandle
The calls are made in the order of the interceptors, and will be called as long as they are configured.
If the programmer decides that the interceptor will intercept the request and then call other interceptors, or the business processor for processing, it returns true.
If the programmer decides that there is no need to call other components to process the request, it returns false.
default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { return true; }
postHandle
Called in reverse order according to the interceptor definition, called after all interceptors in the interceptor chain return successfully.
After the service processor processes the request, DispatcherServlet is called before returning a response to the client.
In this method, the user request is processed.
default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception { }
afterCompletion
Called in reverse order according to the interceptor definition, only called if preHandler returns true.
Called after DispatcherServlet has completely processed the request.
In this method, some resource cleaning operations can be performed.
default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception { }
Interceptor path of action
The action path can be configured in the configuration file.
<mvc:interceptors> <mvc:interceptor>
<!-- 用于指定拦截的url --> <mvc:mapping path="/**" />
<!-- 用于排除指定的 url>
<mvc:exclude-mapping path="" /> <bean id="handlerInterceptorDemo1" class="com.itcast.web.interceptor.HandlerInterceptorDemo1"></bean>
</mvc:interceptor> </mvc:interceptors>