I am trying resource versioning with Spring Mvc 4.I use thymeleaf template engine.But doesnt work with the following code.When load the page I cant see new version Url when i view the page source.So what's the problem in my code? what am i miss?
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/theme*//**").addResourceLocations("/resources/static/theme/")
.setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS))
.resourceChain(false)
.addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"))
.addTransformer(new CssLinkResourceTransformer());
registry.addResourceHandler("/static*//**").addResourceLocations("/resources/static/")
.setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS))
.resourceChain(false)
.addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"))
.addTransformer(new CssLinkResourceTransformer());
registry.addResourceHandler("/static/js*//**").addResourceLocations("/resources/static/js/")
.setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS))
.resourceChain(false)
.addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"))
.addTransformer(new CssLinkResourceTransformer());
}
@Bean
public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
return new ResourceUrlEncodingFilter();
}
I am using with expression in script tag. th:src="@{/resources/static/js/companyList.js}"
Thymeleaf offers a set of Spring integrations that allow you to use it as a fully-featured substitute for JSP in Spring MVC applications.
It provides full integration with Spring Framework. It applies a set of transformations to template files in order to display data or text produced by the application. It is appropriate for serving XHTML/HTML5 in web applications. The goal of Thymeleaf is to provide a stylish and well-formed way of creating templates.
1. Overview. Thymeleaf is a Java template engine for processing and creating HTML, XML, JavaScript, CSS and text. In this tutorial, we will discuss how to use Thymeleaf with Spring along with some basic use cases in the view layer of a Spring MVC application.
From what I read, Thymeleaf is pretty slow compared to other templating languages, while JSP is very fast, with FreeMarker and Velocity coming close. However, Thymeleaf supports natural templates (templates that will render nicely in your browser even if you don't run them through the template engine).
I managed it with no code, only config in application.properties:
# Enable HTML5 application cache manifest rewriting.
spring.resources.chain.html-application-cache=true
# Enable the Spring Resource Handling chain. Disabled by default unless at least one strategy has been enabled.
spring.resources.chain.enabled=true
# Enable the content Version Strategy.
spring.resources.chain.strategy.content.enabled=true
# Comma-separated list of patterns to apply to the Version Strategy.
spring.resources.chain.strategy.content.paths=/**
I didn't need to add any other code to get the hash version in the URLs for CSS and JS.
1. Create Thymeleaf LinkBuilder
that uses Spring's ResourceUrlProvider
to create versioned links:
@Configuration
public class TemplateEngineConfig {
@Autowired
public void configureTemplateEngine(SpringTemplateEngine engine,
ResourceUrlProvider urlProvider) {
engine.setLinkBuilder(new VersioningLinkBuilder(urlProvider));
}
}
class VersioningLinkBuilder extends StandardLinkBuilder {
private final ResourceUrlProvider urlProvider;
VersioningLinkBuilder(ResourceUrlProvider urlProvider) {
this.urlProvider = urlProvider;
}
@Override
public String processLink(IExpressionContext context, String link) {
String lookedUpLink = urlProvider.getForLookupPath(link);
if (lookedUpLink != null) {
return super.processLink(context, lookedUpLink);
} else {
return super.processLink(context, link);
}
}
}
2. Use thymeleaf tags th:href
and th:src
<link th:href="@{/main.css}" rel="stylesheet" type="text/css"/>
<script th:src="@{/js/main.js}" type="text/javascript"></script>
It will be converted to:
<link href="/main-0c362e5c8643b75ddf64940262b219f7.css" rel="stylesheet" type="text/css"/>
<script src="/js/main-c13acb86fa1012e27bbb01a7c4a9bf7f.js" type="text/javascript"></script>
3.(Optional) It is also recommended to add browser cache headers. Add to your application.properties
:
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**
spring.resources.cache.cachecontrol.max-age=365d
spring.resources.cache.cachecontrol.no-cache=false
spring.resources.cache.cachecontrol.no-store=false
spring.resources.cache.cachecontrol.cache-public=true
Or if you use application.yml
:
spring:
resources:
chain:
strategy:
content:
enabled: true
paths: /**
cache:
cachecontrol:
max-age: 365d
no-cache: false
no-store: false
cache-public: true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With