当前位置:   article > 正文

springboot拦截器HandlerInterceptor实现与配置_springboot handlerinterceptor posthandle

springboot handlerinterceptor posthandle

场景:springboot拦截器HandlerInterceptor实现与配置
记录:NO.244
本例环境:
        apache-tomcat-8.5.57
        jdk:1.8
名词
        1.HandlerInterceptor包括preHandle()、postHandle()、afterCompletion()
        preHandle():请求进入Controller前调用。
        postHandle():Controller的方法调用之后执行,在DispatcherServlet进行视图返回渲染之前被调用,所以可以在这个方法中对Controller处理之后的ModelAndView对象进行操作。
        afterCompletion():在请求处理后,在DispatcherServlet渲染了对应的视图之后执行,进行资源清理工作。
        2.拦截器是在Spring容器内组件,是Spring框架支持的组件。配置在SpringMVC中的。
一、springboot版本
        1.配置拦截器

  1. @Configuration
  2. public class PlatformWebMvcConfig implements WebMvcConfigurer {
  3. @Override
  4. public void addInterceptors(InterceptorRegistry registry) {
  5. FirstHandlerInterceptor first = new FirstHandlerInterceptor();
  6. SecondHandlerInterceptor second = new SecondHandlerInterceptor();
  7. ThirdHandlerInterceptor third = new ThirdHandlerInterceptor();
  8. registry.addInterceptor(first).addPathPatterns("/**");
  9. registry.addInterceptor(second).addPathPatterns("/**");
  10. registry.addInterceptor(third).addPathPatterns("/**");
  11. }
  12. }

        2.FirstHandlerInterceptor

  1. public class FirstHandlerInterceptor implements HandlerInterceptor {
  2. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  3. System.out.println("FirstHandlerInterceptor->preHandle执行......");
  4. return true;
  5. }
  6. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
  7. StringBuffer urlBuffer = request.getRequestURL();
  8. System.out.println("FirstHandlerInterceptor->postHandle执行......");
  9. }
  10. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  11. StringBuffer urlBuffer = request.getRequestURL();
  12. System.out.println("FirstHandlerInterceptor->afterCompletion执行......");
  13. }
  14. }

        3.SecondHandlerInterceptor

  1. public class SecondHandlerInterceptor implements HandlerInterceptor {
  2. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  3. System.out.println("SecondHandlerInterceptor->preHandle执行......");
  4. return true;
  5. }
  6. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
  7. System.out.println("SecondHandlerInterceptor->postHandle执行......");
  8. }
  9. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  10. System.out.println("SecondHandlerInterceptor->afterCompletion执行......");
  11. }
  12. }

        4.SecondHandlerInterceptor

  1. public class ThirdHandlerInterceptor implements HandlerInterceptor {
  2. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  3. System.out.println("ThirdHandlerInterceptor->preHandle执行......");
  4. return true;
  5. }
  6. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
  7. System.out.println("ThirdHandlerInterceptor->postHandle执行......");
  8. }
  9. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  10. System.out.println("ThirdHandlerInterceptor->afterCompletion执行......");
  11. }
  12. }

        5.测试Controller

  1. @RestController
  2. @RequestMapping("/fujian")
  3. public class PlatformController {
  4. @GetMapping("/shangban")
  5. public String getShangBan(String num) {
  6. System.out.println("PlatformController->getShangBan执行......");
  7. System.out.println("开始上班......");
  8. System.out.println("上班中......");
  9. System.out.println("报酬 num = " + num + "万");
  10. System.out.println("下班回家......");
  11. return "Hey,GoodJob.";
  12. }
  13. }

        6.测试
        测试URL:  http://localhost:8080/platform/fujian/shangban?num=64
        测试日志:
        
二、xml配置版本
        1.MVC的XML配置新增配置,其它和以上一样。

  1. <!--配置拦截器-->
  2. <mvc:interceptors>
  3. <mvc:interceptor>
  4. <mvc:mapping path="/**"/>
  5. <bean class="com.zbz.interceptor.FirstHandlerInterceptor"/>
  6. </mvc:interceptor>
  7. <mvc:interceptor>
  8. <mvc:mapping path="/**"/>
  9. <bean class="com.zbz.interceptor.SecondHandlerInterceptor"/>
  10. </mvc:interceptor>
  11. <mvc:interceptor>
  12. <mvc:mapping path="/**"/>
  13. <bean class="com.zbz.interceptor.ThirdHandlerInterceptor"/>
  14. </mvc:interceptor>
  15. </mvc:interceptors>

以上,感谢。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/716485
推荐阅读
相关标签
  

闽ICP备14008679号