溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2021-09-28 09:22:48 來源:億速云 閱讀:108 作者:柒染 欄目:大數(shù)據(jù)

今天就跟大家聊聊有關(guān)Springboot2 thymeleaf js/css的版本控制是怎樣的,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

1.啟用版本控制

通過對請求js/css附加md5碼或者手動添加版本號方式來保證在js/css內(nèi)容發(fā)生變更時能及時被瀏覽器加載到:

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修復(fù)

以上方式一般情況下就可以達(dá)到需求效果,但是在實(shí)際開發(fā)過程中由于相對復(fù)雜的場景導(dǎo)致以上配置可能會不生效,通過添加以下Bean就能解決

@SpringBootApplication
public class XxxApplication {

	public static void main(String[] args) {

		SpringApplication.run(XxxApplication.class, args);

	}

	/**
	 * 功能描述
	 * <p>
	 *    添加靜態(tài)資源md5版本控制
	 * </p>
	 *
	 * @author wandoupeas
	 * @date 2019-11-06
	 * @since 2019-11-06
	 */
	@Bean
	public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
		return new ResourceUrlEncodingFilter();
	}
}

文章使用OpenWrite進(jìn)行編寫

看完上述內(nèi)容,你們對Springboot2 thymeleaf js/css的版本控制是怎樣的有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI