gpt4 book ai didi

java - Spring MVC + Thymeleaf : Updates to static resources

转载 作者:搜寻专家 更新时间:2023-11-01 03:24:31 25 4
gpt4 key购买 nike

我的 SpringMVC/Thymeleaf 应用程序遇到了一个问题,当我发布包含对静态资源(图像、CSS 文件、JavaScript 库)的更改的更新时,用户会遇到错误。出现问题是因为他们的浏览器缓存中有旧版本的资源,因此如果他们执行强制刷新,问题就会消失,但大多数用户不知道这样做。

过去,我在其他框架中解决了这个问题,方法是让我的构建将构建号注入(inject) Spring bean 并自定义 URL 构建机制以在 URL 中包含构建号。将此与将这些资源的 Cache-Control 响应 header 设置为遥远的 future 日期相结合,可确保只要用户的浏览器缓存中有资源,就永远不会重新获取,除非我发布新版本。

Spring MVC/Thymeleaf 有解决这个问题的内置方法吗?如果不是,我如何覆盖 Thymeleaf 中的 URL 写入机制,以便我可以添加我上面描述的行为?

最佳答案

一个解决方案可能是编写您自己的方言并定义一个自己的助手 sr。然后你可以写@{#sr(yoururl)}。您会找到有关自己编写的信息 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."

在方言中,您可以定义硬编码的 buildnr。

另一种方法是始终将 buildnr 添加到模型中,然后您可以编写 @{yoururl(buildnr=${buildnr})}

或者更好的做法是将其放入消息源中并使用 @{yoururl(buildnr=#{buildnr})}

关于java - Spring MVC + Thymeleaf : Updates to static resources,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18391585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com