温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Springboot2 thymeleaf js/css的版本控制是怎样的

发布时间:2021-09-28 09:22:48 来源:亿速云 阅读:111 作者:柒染 栏目:大数据

今天就跟大家聊聊有关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的版本控制是怎样的有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI