Springboot2 thymeleaf js/css的版本控制是怎样的
今天就跟大家聊聊有关Springboot2 thymeleaf js/css的版本控制是怎样的,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
1.启用版本控制
通过对请求js/css附加md5码或者手动添加版本号方式来保证在js/css内容发生变更时能及时被浏览器加载到:
yml配置
spring: thymeleaf: mode: HTML cache: false resources: chain: strategy: content: enabled: true paths: /** enabled: true cache: false static-locations: classpath:/static/
或
java配置
@Configuration public class MvcInterceptorConfig implements WebMvcConfigurer { /** * 功能描述 * <p> * .addFixedVersionStrategy("v1.0.1", "/**") 为手动添加版本号方式 * .addContentVersionStrategy("/**") 为md5码方式 * </p> * * @param registry registry * @return void * @author wandoupeas * @date 2019-11-06 * @since 2019-11-06 */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**") .addResourceLocations("classpath:/static/") .resourceChain(false) .addResolver(new VersionResourceResolver() // .addFixedVersionStrategy("v1.0.1", "/**") .addContentVersionStrategy("/**") ); } }
2.Thymeleaf页面引用
正常的abc.js浏览器加载时会变成abc-83fb8c4d9199dce0224da0206423106f.js(md5)或/v1.0.1/abc.js(手动添加版本号)
<!-- css引用 --> <link th:href="@{/abc.css}" rel="stylesheet"> <link th:href="@{/css/def.css}" rel="stylesheet"> <!-- js引用 --> <script th:src="@{/abc.js}"></script> <script th:src="@{/js/def.js}"></script>
3.BUG修复
以上方式一般情况下就可以达到需求效果,但是在实际开发过程中由于相对复杂的场景导致以上配置可能会不生效,通过添加以下Bean
就能解决
@SpringBootApplication public class XxxApplication { public static void main(String[] args) { SpringApplication.run(XxxApplication.class, args); } /** * 功能描述 * <p> * 添加静态资源md5版本控制 * </p> * * @author wandoupeas * @date 2019-11-06 * @since 2019-11-06 */ @Bean public ResourceUrlEncodingFilter resourceUrlEncodingFilter() { return new ResourceUrlEncodingFilter(); } }
文章使用OpenWrite进行编写
看完上述内容,你们对Springboot2 thymeleaf js/css的版本控制是怎样的有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。