[英]Thymeleaf and Static Resources with fingerprint
我嘗試使用ResourceResolvers和ResourceTransformers http://spring.io/blog/2014/07/24/spring-將指紋后綴添加到某些靜態文件(例如,app.min.js / style.min.css)中。 框架4-1處理靜態Web資源
我已經像這樣配置ResourceHandler
@Configuration
@EnableWebMvc
public class ResourceResolverConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("bower_components/**/*.js", "/assets/**", "/build/**")
.addResourceLocations("/bower_components/", "/assets/", "/build/","classpath:/META-INF/webapp/build/")
.resourceChain(true)
.addResolver(
new VersionResourceResolver()
.addContentVersionStrategy("/**")
);
}
}
在我的主控制器中,我添加了一些調試日志。
logger.debug("js = '{}'" + this.resourceUrlProvider.getForLookupPath("/build/app.js"));
logger.debug("css = '{}'" + this.resourceUrlProvider.getForLookupPath("/build/styles/style.css"));
運行Web應用程序后,在調試日志中,每個文件中都有指紋,例如
app-5d2c76ad6517f26d252d5cc93a4fc7d2.js
而且我可以直接訪問此文件(即通過localhost:8080 / build / app-5d2c76ad6517f26d252d5cc93a4fc7d2.js)
但是,當我在Web瀏覽器上單擊查看源時,它仍然是沒有任何指紋的原始文件。
我在layout.html文件中這樣加載腳本/鏈接。
<script th:src="@{/build/app.js}"></script>
<link th:href="@{/build/styles/style.css}" rel="stylesheet"></link>
我將Thymeleaf用於模板引擎。 要使Thymeleaf包含指紋文件,應該使用什么配置或代碼?或者我錯過了什么?
非常感謝你。
確保您包含適當的編碼過濾器,這是@{}
語法用於重寫URL的內容:
@Bean
public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
return new ResourceUrlEncodingFilter();
}
嘗試此操作以呈現指紋以及文件名
<script th:src="${@mvcResourceUrlProvider.getForLookupPath('/build/app.js')}" />
上面的結果是:
<script src="/build/app-16e85c31092c68733df3c729b831dcfd.js"></script>
我知道這很晚了,但是我一直在尋找相同的答案,以了解如何呈現與addContentVersionStrategy一起使用的url,這很有用,因此希望對其他人有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.