In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how to use Java's SpringMVC interceptor. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Function of interceptor (interceptor)
Spring MVC's interceptor is similar to the filter Filter in Servlet development and is used for pre-processing and post-processing of processors. The interceptor is connected into a chain in a certain order, which is called the interceptor chain (Interceptor Chain). When accessing the intercepted method or field, the interceptors in the interceptor chain are called in the order they were previously defined. Interceptor is also a concrete implementation of AOP idea.
The scope of use of interceptor and filter distinguishes filter (Filter) interceptor (Intercepter) is part of the servlet specification. Any Java Web project can be used by the SpringMVC framework itself. Only projects that use the SpringMVC framework can use the intercept scope to intercept. After / * is configured in url-pattern, you can intercept all the resources to be accessed after / * * is configured in, or you can intercept more resources. But you can get started with the tag to eliminate resource interceptors that do not need to be intercepted.
Custom intercept steps:
① creates interceptor class to implement HandlerInterceptor interface
Public class MyHandlerInterceptor1 implements HandlerInterceptor {public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) {System.out.println ("preHandle running..."); return true;} public void postHandle (HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {System.out.println ("postHandle running...") } public void afterCompletion (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {System.out.println ("afterCompletion running...");}}
② configuration interceptor
③ tests the interception effect of the interceptor (writing target methods)
@ RequestMapping ("/ quick23") @ ResponseBodypublic ModelAndView quickMethod23 () throws IOException, ParseException {System.out.println ("Target method execution...."); ModelAndView modelAndView = new ModelAndView (); modelAndView.addObject ("name", "modelname"); modelAndView.setViewName ("index"); return modelAndView;}
(visit the website)
Http://localhost:8080/project/quick23
Console print results
Multi-interceptor operation
The steps are the same as above, in writing a MyHandlerInterceptor2 operation, the test execution order can be done.
Interceptor method description method name indicates that the preHandle () method will be called before the request is processed. The return value of this method is of Boolean Boolean type. When it returns false, the request ends, and subsequent Interceptor and Controller will not be executed. When the return value is true, the preHandle square method postHandle () of the next Interceptor will be called. This method is called after the current request has been processed, provided that the return value of the preHandle method is true, and it will be called before the view return rendering of the DispatcherServlet, so we can operate on the ModelAndView object after Controller processing in this method. This method will be called after the whole request is completed. That is, it is executed after DispatcherServlet renders the corresponding view, and only if the return value of the preHandle method is true can it be called. This is all the content of the article "how to use Java's SpringMVC interceptor". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.