注释掉的是失败的代码,失败原因:配置类继承了WebMvcConfigurationSupport,并重写了里面的方法。之后的拦截器配置类同样继承了这个这个类并重写方法,它只会生效前一个配置类,后一个配置类不会生效,所以解决方法就是在一个配置类重写这两个方法就行了,不要分成两个配置类写。但是看着
package com.example.config.config;
import com.example.config.component.LoginHandleIntercept;
import com.example.config.component.MyLocaleResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.*;
//使用WebMvcConfigurationSupport可以扩展SpringMvc功能
@Configuration
public class MyMvcConfig extends WebMvcConfigurationSupport {
@Override
protected void addViewControllers(ViewControllerRegistry registry) {
super.addViewControllers(registry);
//浏览器发送/atguigu请求来到success
registry.addViewController("/atguigu").setViewName("success");
registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("login");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}
@Override
protected void addInterceptors(InterceptorRegistry registry) {
//super.addInterceptors(registry);
registry.addInterceptor(new LoginHandleIntercept()).addPathPatterns("/**").excludePathPatterns(
"/index.html","/","/user/login"
);
}
// @Bean
// public WebMvcConfigurationSupport webMvcConfigurationSupport(){
// WebMvcConfigurationSupport support = new WebMvcConfigurationSupport(){
// //注册拦截器
// @Override
// protected void addInterceptors(InterceptorRegistry registry) {
// // super.addInterceptors(registry);
// //springboot已经做好了静态资源的映射
// registry.addInterceptor(new LoginHandleIntercept()).addPathPatterns("/**").excludePathPatterns(
// "/index.html","/","/user/login"
// );
// }
// @Override
// protected void addViewControllers(ViewControllerRegistry registry) {
//// super.addViewControllers(registry);
// registry.addViewController("/").setViewName("login");
// registry.addViewController("/index.html").setViewName("login");
// }
// };
// return support;
// }
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}