springboot无法访问静态资源

时间:2023-03-09 09:21:44
springboot无法访问静态资源

无法访问static下的静态资源

springboot无法访问静态资源

1.在application.yml中添加

  resources:
    static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:./../images

2、配置webconfig

  

@EnableWebMvc
@Configuration
public class WebAppConfig implements WebMvcConfigurer { private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/" }; @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new AppInterceptor()).addPathPatterns("/**").excludePathPatterns("");
} @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//配置server虚拟路径,handler为页面中访问的目录,locations为files相对应的本地路径
registry.addResourceHandler("/static/**")
.addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
} }
ok,可以访问静态资源了