您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)Springboot2 thymeleaf js/css的版本控制是怎樣的,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
通過對請求js/css附加md5碼或者手動添加版本號方式來保證在js/css內(nèi)容發(fā)生變更時能及時被瀏覽器加載到:
spring: thymeleaf: mode: HTML cache: false resources: chain: strategy: content: enabled: true paths: /** enabled: true cache: false static-locations: classpath:/static/
或
@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("/**") ); } }
正常的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>
以上方式一般情況下就可以達(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è)資訊頻道,感謝大家的支持。
免責(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)容。