SpringBoot-静态文件加载失败-改写static路径

SpringBoot-静态文件加载失败-改写static路径

  • Spring Boot 对静态资源映射提供了默认配置

         Spring Boot 默认将 /** 所有访问映射到以下目录: 
         classpath:/static 
         classpath:/public 
         classpath:/resources 
         classpath:/META-INF/resources 
         Spring Boot 默认会挨个从 public resources static 里面找是否存在相应的资源,如果有则直接返回。 

  • 自定义静态资源映射 

        如果改写了static的路径,找不到静态资源了,别忘记配置静态资源映射

package core_web.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //将所有/static/** 访问都映射到classpath:/static/ 目录下
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
}
  •  这样配置在resoutces下的static会被发现资源

  • 测试静态文件

static 如果和 resoutces 是平级标签,如果没有更改静态映射,是可以访问的

随笔记录,方便自己学习

chenyb 2018-11-20

猜你喜欢

转载自blog.csdn.net/scdncby/article/details/84302357