为什么需要设置访问过滤?
还记得之前在ServletContainersInitConfig这个类里面定义了return new String[]{"/"};
吧,这是将所有的东西都交给springmvc管理了,现在tomcat都管不到那些静态资源了。
怎么进行过滤?
这里需要我们再定义一个类SpringMvcSupport
,这个SpringMvcSupport
类专门去做发行这个工作。还要注意啊,这里需要声明它是一个配置类。
代码:
@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {
//设置静态资源访问过滤,当前类需要设置为配置类,并被扫描加载
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
//当访问/pages/????时候,从/pages目录下查找内容
registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
}
}