springboot无法访问静态资源

无法访问static下的静态资源

 

 

 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,可以访问静态资源了
posted @ 2019-11-21 11:04  柴米油盐哈哈  阅读(3939)  评论(1编辑  收藏  举报