攔截器
- 建立攔截器
/** * @author wsyjlly * @create 2019.06.13 - 16:52 **/ public class FileUploadInterceptor implements HandlerInterceptor { private Logger logger = LoggerFactory.getLogger(getClass()); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { logger.debug("FileUploadInterceptor...preHandle"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { logger.debug("FileUploadInterceptor...postHandle"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { logger.debug("FileUploadInterceptor...afterCompletion"); } }
- 注冊攔截器
- 注冊方式一:一般注冊方式
/** * @author wsyjlly * @create 2019.06.12 - 16:17 **/ @Configuration public class WebMvcConfig implements WebMvcConfigurer { /* * 配置攔截器 * */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new FileUploadInterceptor()). addPathPatterns("/upload").order(1); } }
- 注冊方式二:當攔截器中需要注入變量時須提前加載該攔截器
/** * @author wsyjlly * @create 2019.06.12 - 16:17 **/ @Configuration public class WebMvcConfig implements WebMvcConfigurer { /* * 配置攔截器 * */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(getFileUploadInterceptor()). addPathPatterns("/upload").order(1); } @Bean public FileUploadInterceptor getFileUploadInterceptor(){ return new FileUploadInterceptor(); } }
- 注冊方式一:一般注冊方式