Spring's HandlerMapping supports interceptor, and the interceptor must implement the HandlerInterceptor interface. There are the following 3 methods in this interface:
1. The preHandle() processor is called before execution. The method returns true to continue calling other interceptors or processors. Return false to indicate the interruption process. The subsequent interceptors and processors will no longer be executed.
2. After the postHandle() processor is executed, it is called before the view is executed. At this time, the data model data or the view is processed through the ModelAndView object.
3. Called after the entire process of Completion(). For example, in performance monitoring, we can record the end time and output the time consumed here, and we can also write a cleanup of resources here, but the afterCompletion method will be executed only when preHandle() returns true.
public class someInterceptor implements HandlerInterceptor{ public boolean preHandle(HttpServletRequest req,HttpServletResponse resp,Object handler)throws Exception{ //The TODO processor is called before execution return true; } public void postHandle(HttpServletRequest req,HttpServletResponse resp,Object handler,ModelAndView mv)throws Exception{ //The TODO processor is called after execution return true; } public void afterCompletion(HttpServletRequest req,HttpServletResponse resp,Object handler,Exception e)throws Exception{ //Call after TODO is completed}}This custom interceptor implements the HandlerInterceptor interface and implements all methods in the interface. If you only want to use a certain method, you can inherit the HandlerInterceptorAdapter.
Interceptor configuration
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/org/*"/> <mvc:exclude-mapping path="/login/*"/> <bean/> </mvc:interceptor></mvc:interceptors>
Summarize
The above is all the content of this article about the code analysis of the Spring Interceptor interface code, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!