Springboot 之 静态资源路径配置

1、静态资源路径是指系统可以直接访问的路径,且路径下的所有文件均可被用户通过浏览器直接读取。

2、在Springboot中默认的静态资源路径有:classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

3、在Springboot中可以直接在配置文件中覆盖默认的静态资源路径的配置信息:

复制代码
#自定义的属性,指定了一个路径,注意要以/结尾
web.upload-path=D:/temp/study13/
#表示所有的访问都经过静态资源路径 spring.mvc.static-path-pattern=/**

#覆盖默认配置,所以需要将默认的也加上否则static、public等这些路径将不能被当作静态资源路径
#在最末尾的file:${web.upload-path}中的file:表示是一个具体的硬盘路径,其他的使用classpath指的是系统环境变量
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${web.upload-path}
复制代码

4、在SpringBoot开发中,可以在Java代码中覆盖默认静态资源配置

复制代码
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

@Override
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> addResourceHandlers(ResourceHandlerRegistry registry) {

    </span><span style="color: #0000ff">if</span>(!registry.hasMappingForPattern(<span style="color: #800000">"</span><span style="color: #800000">/static/**</span><span style="color: #800000">"</span><span style="color: #000000">)){
        registry.addResourceHandler(</span><span style="color: #800000">"</span><span style="color: #800000">/static/**</span><span style="color: #800000">"</span>).addResourceLocations(<span style="color: #800000">"</span><span style="color: #800000">classpath:/static/</span><span style="color: #800000">"</span><span style="color: #000000">);
    }
    super.addResourceHandlers(registry);
}

}

复制代码

 5、由于Spring Boot 默认资源路径配置的问题,使用IDEA开发Spring Boot应用时,会导致一个问题————浏览器、编辑器 不能同时访问 JS 等资源的问题。这时往往通过配置 4 中的代码,来实现同时访问资源文件的效果

 

参考知识林:http://www.zslin.com/web/article/detail/23

分类: Spring Boot
标签: Spring Boot
0
0
« 上一篇: Java 时间不一致
» 下一篇: Hibernate 数据库方言
	</div>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值