In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the function of java interceptor". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the role of java interceptor".
1. Interceptor class: IndexInterceptor
Package interceptor;import java.util.Date; import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.handler.HandlerInterceptorAdapter Public class IndexInterceptor extends HandlerInterceptorAdapter {/ * is called before the business processor processes the request * if false is returned * execute afterCompletion () of all interceptors back from the current interceptor and then exit the interceptor chain * if true is returned * execute the next interceptor Until all interceptors have been executed * execute intercepted Controller * then enter the interceptor chain, * execute all postHandle () back from the last interceptor * and then execute all afterCompletion () / public boolean preHandle (HttpServletRequest request, HttpServletResponse response) back from the last interceptor. Object handler) throws Exception {System.out.println ("preHandle (), called before accessing Controller") Return true } / * * after the execution of the request processed by the business processor, the actions performed before generating the view * can add data to the modelAndView For example, the current time * / public void postHandle (HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println ("postHandle ()) is called after accessing the Controller and before accessing the view. Here you can inject a time into the modelAndView for subsequent view display") ModelAndView.addObject ("date", "time generated by interceptor:" + new Date ()) } / * is called after the DispatcherServlet has fully processed the request, and can be used to clean up resources, etc. * * when an interceptor throws an exception, afterCompletion () / public void afterCompletion (HttpServletRequest request, HttpServletResponse response, Object handler) of all interceptors will be executed back from the current interceptor. Exception ex) throws Exception {System.out.println ("afterCompletion (), called after accessing the view") }}
2. Configure the interceptor
Modify springmvc-servlet.xml to intercept the / index path
If you want to block other paths:
/ * * intercept all
/ category/** intercept / all under the category path
IndexController
3. Modify the index.jsp to print the date the interceptor put in.
${message}
${date}
Thank you for reading, the above is the content of "what is the function of java interceptor". After the study of this article, I believe you have a deeper understanding of what the role of java interceptor is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.