拦截器

  1. • @author yuzb
  2. • @date 2020/5/20 13:59
  3. • @description
  4. */
  5. public class MyInterceptor implements HandlerInterceptor {
  6. /*
  7. • @Description 该方法在控制器的处理请求方法前执行
  8. • @Return boolean true 表示继续向下执行,返回 false 表示中断后续
  9. • @Author yuzb
  10. • @throws Exception
  11. • @Date 2020/5/20 14:02
  12. **/
  13. @Override
  14. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  15. System.out.println("preHandle方法在控制器的处理请求方法调用之后,解析视图之前执行");
  16. return true;
  17. }
  18. /*
  19. • @Description 解析试图视图之前调用
  20. • @Return void
  21. • @Author yuzb
  22. • @throws Exception
  23. • @Date 2020/5/20 14:03
  24. **/
  25. @Override
  26. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
  27. System.out.println("postHandle方法在控制器的处理请求方法调用之后,解析视图之前执行");
  28. }
  29. /*
  30. • @Description 视图渲染结束后执行
  31. • @Return void
  32. • @Author yuzb
  33. • @throws Exception
  34. • @Date 2020/5/20 14:04
  35. **/
  36. @Override
  37. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  38. System.out.println("afterCompletion方法在控制器的处理请求方法执行完成后执行,即视图渲染结束之后执行");
  39. }
  40. }
  41. 注册拦截器
  42. /**
  43. * @author yuzb
  44. * @date 2020/5/20 14:06
  45. * @description
  46. */
  47. @Configuration
  48. public class WebMvcConfig implements WebMvcConfigurer {
  49. @Override
  50. public void addInterceptors(InterceptorRegistry registry) {
  51. registry.addInterceptor(myInterceptor()).addPathPatterns("/test/**");
  52. }
  53. @Bean
  54. MyInterceptor myInterceptor() {
  55. return new MyInterceptor();
  56. }
  57. }