直接上代码
第一步:其中重写 addResourceHandlers 方法很重要
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Bean //将组件注册在容器
public WebMvcConfigurer webMvcConfigurerAdapter(){
WebMvcConfigurer adapter = new WebMvcConfigurer() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/login.html").setViewName("index");
}
//注册拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/login.html", "/user/login", "/","/static/**","/**/*.css");
} //就是不能放行css,我也很纳闷
//放行资源必须配置的
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/");
}
};
return adapter;
}
}
第二步: css引入资源的前面加上/static
th:href="@{/static/a/css/b.css}"