In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today I will show you how to implement the interceptor in SpringMVC. The content of the article is good. Now I would like to share it with you. Friends who feel in need can understand it. I hope it will be helpful to you. Let's read it along with the editor's ideas.
The Interceptor interceptor in SpringMVC is very important and useful. Its main function is to intercept specified user requests and carry out corresponding preprocessing and post-processing.
The point in time of its interception is that "the processor mapper maps the processor class to be executed according to the request submitted by the user, and also finds the processor adapter to execute the processor class, before the processor adapter executes the processor."
Of course, when the processor mapper maps the processor class to be executed, the interceptor and the processor are combined into a processor execution chain and returned to the central scheduler.
Interceptors and filters are very similar, but there is a big difference.
Filters are part of the servlet specification and can be used by any javaweb tool.
The filter is based on callback functions.
Interceptor is a concrete application of spring's AOP idea, and interceptor is based on reflection mechanism.
The interceptor is springmvc's own and can only be used by springmvc framework projects.
Interceptors will only intercept access control methods, for static resources, resources without controller are not used.
It is important that the interceptor can get each bean in the IOC container, but not the filter. Inject a service into the interceptor, but
To invoke the business logic.
Implementation of interceptor
Custom interceptor, you need to implement the HandlerInterceptor interface. There are three methods in this interface:
➢ preHandle (request,response, Object handler):
This method is executed before the processor method is executed. The return value is boolean, and if true, the processor method is executed immediately after, and the afterCompletion () method is placed on a special method stack waiting for execution.
➢ postHandle (request,response, Object handler,modelAndView):
This method is executed after the processor method is executed. If the processor method is ultimately not executed, the method will not be executed.
Because the method is executed after the processor method is executed, and the method parameter contains ModelAndView, the method can modify the processing result data of the processor method and can modify the jump direction.
➢ afterCompletion (request,response, Object handler, Exception ex): when the preHandle () method returns true, the method is placed in a special method stack until all that respond to the request
The method is not executed until the work is done. That is, the method is executed after the central scheduler has rendered (populated with data) the response page, and it is useless to operate on the ModelAndView at this time.
The last method executed by afterCompletion to clear resources, such as adding data to the Controller method
Register interceptor: package com.dongmu.interceptor;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class Interceptor implements HandlerInterceptor {@ Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println ("interceptor intercepts before processor method execution.") ; return true;} public void postHandle (HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println ("interceptor performs post-processing on processor methods"); public void afterCompletion (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println ("methods that interceptors must execute");}
When there are multiple interceptors, a chain of interceptors is formed. The execution order of the interceptor chain is the same as its registration order. It is important to emphasize again that when the preHandle () method of an interceptor returns true and is executed, the interceptor's afterCompletion () method is put into a special method stack.
Note: every request through servlet, the processor controller, goes through the interceptor. (in the case of specified path)
This is what the implementation of interceptor in SpringMVC is all about. For more information related to the implementation of interceptor in SpringMVC, you can search the previous articles or browse the following articles to learn! I believe the editor will add more knowledge to you. I hope you can support it!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.