最近因為換了一個從SpringMVC改造過來的SpringBoot開發(fā)框架蔽介,發(fā)現(xiàn)配置了static-locations
死活未生效淮腾,苦尋未覓糟需。
例如我的配置:
spring:
resources:
static-locations: file:D:/static/
最后百度才知道,有可能是MVC的配置中添加了攔截:
找到繼承WebMvcConfigurer
的配置類
@Configuration
@EnableWebMvc
public class SpringMVCConfig implements WebMvcConfigurer
查看攔截:
@Override
public void addInterceptors(InterceptorRegistry registry){
registry.addInterceptor(new DefaultInterceptorQx())
.addPathPatterns("/app/**");
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/", "/index.html");
registry.addRedirectViewController("/index", "/index.html");
}
只需要在該類中重寫public void addResourceHandlers(ResourceHandlerRegistry registry)
方法谷朝,并將靜態(tài)文件地址添加進來即可:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
try {
// 或者從配置文件中取static-locations
registry.addResourceHandler("/**")
.addResourceLocations("file:d:\\static\\")
.setCachePeriod(31556926);
} catch (IOException e) {
e.printStackTrace();
}
}
參考:https://blog.csdn.net/zxc1310284454/article/details/81127889