1、使用@EnableWebMvc注解开启SpringMVC定制配置功能,相当于<mvc:annotation-driven/>
①实现WebMvcConfigurer接口,并重写该接口中的方法(高版本的Spring中该接口中的方法不一定必须实现)
@EnableWebMvc
@ComponentScan(value = "com.bdm", useDefaultFilters = false, includeFilters = {@Filter(type = FilterType.ANNOTATION, classes = { Controller.class }) })
public class AppConfig implements WebMvcConfigurer {public void configurePathMatch(PathMatchConfigurer configurer) {}public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {}public void configureAsyncSupport(AsyncSupportConfigurer configurer) {}public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {configurer.enable();//相当于<mvc:default-servlet-handler/>,将SpringMVC处理不了的请求交给tomcat,比如静态资源的访问}public void addFormatters(FormatterRegistry registry) {}public void addInterceptors(InterceptorRegistry registry) {}public void addResourceHandlers(ResourceHandlerRegistry registry) {}public void addCorsMappings(CorsRegistry registry) {}public void addViewControllers(ViewControllerRegistry registry) {}public void configureViewResolvers(ViewResolverRegistry registry) {}public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {}public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> handlers) {}public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {}public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {}public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {}public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {}@Nullablepublic Validator getValidator() {return null;}@Nullablepublic MessageCodesResolver getMessageCodesResolver() {return null;}
}
②继承WebMvcConfigurerAdapter,高版本中已不建议使用这种方式,因为和实现WebMvcConfigurer接口一样
@SuppressWarnings("deprecation")
@EnableWebMvc
@ComponentScan(value = "com.bdm", useDefaultFilters = false, includeFilters = {@Filter(type = FilterType.ANNOTATION, classes = { Controller.class }) })
public class AppConfig extends WebMvcConfigurerAdapter {@Overridepublic void configureViewResolvers(ViewResolverRegistry registry) {// 配置视图解析器// registry.jsp();//内部调用了jsp("/WEB-INF/", ".jsp");从/WEB-INF/中找jspregistry.jsp("/WEB-INF/pages/", ".jsp");}@Overridepublic void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {configurer.enable();// 配置静态资源访问,不开启则静态资源无法访问}@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");// 添加自定义的拦截器和拦截的请求}
}
自定义的拦截器:
public class MyInterceptor implements HandlerInterceptor {// 目标方法运行之前执行public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {System.out.println("preHandle...");return true;//return true才会继续往下执行请求方法,否则请求方法得不到执行}// 目标方法执行正确之后执行public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,@Nullable ModelAndView modelAndView) throws Exception {System.out.println("postHandle...");}// 页面响应之后执行public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,@Nullable Exception ex) throws Exception {System.out.println("afterCompletion...");}
}