In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The content of this article mainly focuses on the use of SpringMVC HandlerInterceptor interceptor and what are the parameters. The content of the article is clear and clear. It is very suitable for beginners to learn and is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!
Interceptor concept:
Interceptor (Interceptor) is a mechanism for dynamically intercepting method calls, and the request processing process is parsed.
Core principle: AOP thought
Interceptor chain: multiple interceptors enhance the original called function in a certain order
Function:
Executes the predetermined code before and after the specified method call
Prevent the execution of the original method
Interceptor VS filter
Different attribution: filter belongs to Servlet technology, interceptor belongs to SpringMVC technology to intercept content is different: filter enhances all access, interceptor enhances access only for SpringMVC
Interceptor execution process:
Customize the interceptor development process:
Implement the HandlerInterceptor interface
/ / Custom interceptor needs to implement HandleInterceptor interface public class MyInterceptor implements HandlerInterceptor {/ / pre-processing method: execute @ Override public boolean preHandle (HttpServletRequest request, / / request object HttpServletResponse response, / / response object Object handler) / / called processor object before the original method, which is essentially a method object The Method object in reflection is repackaged, the method is encapsulated and enhanced, and the original object is manipulated, throws Exception {System.out.println ("pre-run") / / the return value of false will intercept the operation of the original processor, that is, whether to let it go. If the code behind false will not run, if it is true, continue to execute the following code / / if multiple interceptors are configured, the return value of false will terminate the running return true of the interceptor configured after the current interceptor } / / Post processing method: run after the original method is run. If the original method is intercepted, @ Override public void postHandle (HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) is not executed / / if the processor completes execution, the result is returned. You can read the corresponding data and page information to adjust throws Exception {System.out.println ("post run") } / / completion method: the last method executed by the interceptor, regardless of whether the original method executes @ Override public void afterCompletion (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) / / if an exception object occurs during processor execution Throws Exception {System.out.println ("finish running") can be handled separately for exception cases. } / / the running order of the three methods is preHandle-> postHandle-> afterCompletion / / if the return value of preHandle is false, the three methods only run preHandle} interceptor configuration item: multi-interceptor configuration:
Running order: if it is configured first, it will be executed first.
Multi-interceptor execution order:
Responsibility chain model
The mode of responsibility chain is a kind of behavior mode.
Characteristics:
Execute sequentially along a predetermined chain of tasks, with each node having an independent task
Advantages:
Independence: only focus on the tasks of the current node, and let other tasks go directly to the next node
Isolation: with chain delivery characteristics, you don't need to know the overall link structure, you just need to wait for the request to arrive and process it.
Flexibility: the link structure can be arbitrarily modified to dynamically add or delete the overall link responsibility
Decoupling: decoupling dynamic tasks from original tasks
Disadvantages:
When the link is too long, the processing efficiency is low.
There may be circular references on the nodes, causing an endless loop and causing the system to crash.
Thank you for your reading. I believe you have some understanding of "what are the use and parameters of SpringMVC HandlerInterceptor interceptor". Go ahead and practice it. If you want to know more about it, you can follow the website! The editor will continue to bring you better articles!
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: 264
*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.