在我们做Web开发时,免不了与静态资源(css,js,images)打交道,通常的做法是在请求的URL上添加版本信息,这样可以很好的利用客户端缓存机制,只有当资源内容改变时,才需要从服务器重新请求,并加载最新版本资源。

Spring的静态资源管理

ResourceUrlProvider

首先,MVC中增加资源处理器

@Configuration
public class MvcApplication extends WebMvcConfigurerAdapter {@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {VersionResourceResolver versionResourceResolver = new VersionResourceResolver().addVersionStrategy(new ContentVersionStrategy(), "/**");registry.addResourceHandler("/javascript/*.js").addResourceLocations("classpath:/static/").setCachePeriod(60 * 60 * 24 * 365) /* one year */.resourceChain(true).addResolver(versionResourceResolver);}...
}

在SpringBoot中就可以用如下配置

spring:resources:chain:enabled: truecache: trueversion:content:enabled: truepaths: /static/**fixed:enabled: trueversion: 1.0paths: /js/**,/foo/**

或者

spring:resources:chain:enabled: truecache: trueversion.content:enabled: truepaths: /static/**version.fixed:enabled: trueversion: 1.0paths: /js/**,/foo/**

静态资源路径请根据实际路径修改

**ContentVersionStrategy**MD5策略,如,请求

/javascript/test-69ea0cf3b5941340f06ea65583193168.js

则被解析为:

/javascript/test.js

FixedVersionStrategy固定版本策略,如

/v1.2.3/javascript/test.js

使用ResourceUrlProvider生成包含MD5信息

With a ResourceUrlProvider a resource URL (e.g. /javascript/test.js) can be converted to a versioned URL (e.g. /javascript/test-69ea0cf3b5941340f06ea65583193168.js). A ResourceUrlProvider bean with the id mvcResourceUrlProvider is automatically declared with the MVC configuration.

如果你使用Thymeleaf模板引擎(可以直接访问ResourceUrlProvider Bean),那么前端页面你可以直接这么使用:

<script type="application/javascript"th:src="${@mvcResourceUrlProvider.getForLookupPath('/javascript/test.js')}">
</script>

如果你使用其他例如FreeMarker等模板引擎,你可以通过@ControllerAdvice将ResourceUrlProvider Bean添加到Model Attribute中,就像这么使用:


@ControllerAdvice
public class ResourceUrlAdvice {@InjectResourceUrlProvider resourceUrlProvider;@ModelAttribute("urls")public ResourceUrlProvider urls() {return this.resourceUrlProvider;}
}

在这种情况下,前端页面可以这么写

<script type="application/javascript" th:src="${urls.getForLookupPath('/javascript/test.js')}">
</script>

这种方式支持所有的模板引擎。

ResourceUrlEncodingFilter

另一种方式是通过ResourceUrlEncodingFilter,其是Servlet Filter,通过重写HttpServletResponse.encodeURL()来生成版本化的资源地址。使用起来也很简单,配置项中增加ResourceUrlEncodingFilter Bean,如下:

@SpringBootApplication
public class MvcApplication extends WebMvcConfigurerAdapter {@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {// same as before ..}@Beanpublic ResourceUrlEncodingFilter resourceUrlEncodingFilter() {return new ResourceUrlEncodingFilter();}...
}

如果你正在使用的模板引擎(如:JSPs, Thymeleaf, FreeMarker and Velocity)支持直接调用response.encodeURL()方法,那么版本信息将自动被添加到资源URL中。

例如,在Thymeleaf中,我们可以用@{..} 语法

<script type="application/javascript" th:src="@{/javascript/test.js}"></script>

那么最终页面将会如下:

<script type="application/javascript" src="/javascript/test-69ea0cf3b5941340f06ea65583193168.js">
</script>

SpringBoot 1.3.0版本之后,使用* ResourceUrlEncodingFilter*将更加简单

As of Spring Boot 1.3.0, this is now even easier. The Resource chain can be configured via Boot properties and the ResourceUrlEncodingFilter is automatically registered

Links to resources are rewritten at runtime in template, thanks to a ResourceUrlEncodingFilter, auto-configured for Thymeleaf and Velocity. You should manually declare this filter when using JSPs. Other template engines aren’t automatically supported right now, but can be with custom template macros/helpers and the use of the ResourceUrlProvider.

https://github.com/spring-projects/spring-boot/pull/3123

https://dzone.com/articles/2-step-resource-versioning-with-spring-mvc

SpringBoot 静态资源版本管理相关推荐

  1. 第14章 SpringBoot静态资源处理

    第14章 SpringBoot静态资源处理 14.1 WebMvcAutoConfiguration的默认配置 14.2 自定义静态资源映射 14.3 前端资源的引用方法

  2. springBoot静态资源优先级)

    springBoot静态资源优先级 springboot项目结构 默认优先级 自己设置指定某目录内文件为静态资源 springboot项目结构 默认优先级 /META-INF/resources> ...

  3. springboot 静态资源访问,和文件上传 ,以及路径问题

    springboot 静态资源访问: 这是springboot 默认的静态资源访问路径  访问顺序依次从前到后(http://localhost:8080/bb.jpg) spring.resourc ...

  4. SpringBoot - 静态资源映射处理

    SpringBoot - 静态资源映射处理 [1]静态资源文件映射规则 同样查看WebMVCAutoConfiguration源码如下: @Overridepublic void addResourc ...

  5. SpringBoot静态资源目录

    SpringBoot静态资源目录 前言 今天博主将为大家分享SpringBoot静态资源目录!不喜勿喷,如有异议欢迎讨论! 以下所写内容均与以前的文章有联系可以前往博文查看,陈永佳的博客 之前的一系列 ...

  6. springboot 静态资源缓存设置

    springboot 静态资源 js css  缓存设置 @Configuration public class WebMvcConfiguration implements WebMvcConfig ...

  7. springboot静态资源访问

    springboot的项目中,默认的开启的静态资源目录有: classpath:/META-INF/resources/, classpath:/resources/, classpath:/stat ...

  8. springBoot静态资源处理

    Spring Boot 静态资源处理 spring boot项目如果要展示静态页面,可以把html.js.css等文件放在resources目录下的static或public目录里面(如果没有可以直接 ...

  9. SpringBoot静态资源的映射

    一, webjars 所有的webjars被导入后,目录结构都是这样的 : springboot的底层告诉我们 , 如果要引用webjars , 我们只需要在引用的位置使用 " /webja ...

  10. springboot静态资源的配置

    1. springboot默认的静态资源存放路径 静态资源的存放路径为classpath,也就是resources目录下的: /META-INF/resources /resources /stati ...

最新文章

  1. Android开发更改应用图标无效的问题
  2. Spring Boot + MDC 实现全链路调用日志跟踪
  3. linux C语言 socket编程教程(附两个例子)(socket教程)
  4. Mysql UTF8 varchar与Oracle ZHS16GBK varchar2同长度下存汉字的差异
  5. 使用 SASS Mixin 编写 clean code
  6. 程序员自家种水果,新鲜包邮配送
  7. android strm,Android 关于so文件的随记
  8. 无法序列化会话状态。请注意,当会话状态模式为“StateServer”或“SQLServer”时,不允许使用无法序列化的对象或 MarshalByRef 对象。...
  9. ML.NET 发布0.11版本:.NET中的机器学习,具有TensorFlow和ONNX的新功能
  10. 添加下拉框00-23 finereport公式
  11. linux中#和## 用法
  12. button图片与文字的布局
  13. c++ 写一个复数计算器
  14. 一个小程序走完诉讼全程,腾讯云加速推动“智慧法院”方案落地
  15. 关于外国人报考美国专利代理人的基础介绍
  16. 微信小程序设置字体无效_微信小程序字体设置
  17. 浪潮飞龙系列国产服务器
  18. 【C语言】PAT乙级1004 成绩排名
  19. ifconfig与 ip addr命令详细
  20. 痞子衡嵌入式:串口调试工具Jays-PyCOM诞生记(1)- 环境搭建(Python2.7.14 + pySerial3.4 + wxPython4.0.3)...

热门文章

  1. 复旦大学2018--2019学年第二学期(18级)高等代数II期末考试第八大题解答
  2. LM324运放器应用实例
  3. (Python编程)稳定的copra算法(copra-ep)
  4. Optimizing graphics rendering in Unity games
  5. 摄影测量学知识点总结(万字长文警告)
  6. java毕业设计——基于java+JSP+J2EE的户籍管理系统设计与实现(毕业论文+程序源码)——户籍管理系统
  7. 自己动手——快速搭建Java应用服务器
  8. 我的iPad/iPhone App推荐列表
  9. 安卓psp模拟器联机教程_psp模拟器联机平台下载
  10. 冰点还原离线激活_冰点还原密钥,小编教你如何激活冰点还原