简体   繁体   中英

Spring MVC + Thymeleaf: Updates to static resources

I'm running into an issue with my SpringMVC/Thymeleaf application where users experience errors when I release an update that includes changes to static resources (images, CSS files, JavaScript libraries). The problem occurs because they have an old version of the resource in their browser cache, so if they do a force refresh the problem goes away, but most users don't know to do that.

In the past, I've solved this in other frameworks by having my build inject the build number into a Spring bean and customizing the URL building mechanism to include the build number in the URL. Combining this with setting the Cache-Control response header to a far-future date on these resources ensures that as long as the user has the resource in their browser cache, it is never re-fetched unless I release a new build.

Does Spring MVC/Thymeleaf have a built-in way of solving this problem? If not, how do I override the URL writing mechanism in Thymeleaf so that I can add the behavior I described above?

One solution could be to write your own dialect and define an own helper sr. Then you can write @{#sr(yoururl)} . You find information about writing your own dialect .

"As for creating your own utility object, that can be done by creating a simple dialect that implements the IExpressionEnhancingDialect interface. That will make Thymeleaf add any objects returned by your implementation of the getAdditionalExpressionObjects() method to the processing context."

In the dialect you could define the buildnr hard coded.

Another approach is to add the buildnr always to the model, then you can write @{yoururl(buildnr=${buildnr})} .

Or better you put it into the message source and can use @{yoururl(buildnr=#{buildnr})} .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM