![](/img/trans.png)
[英]Spring boot and Thymeleaf - Hot swap templates and resources once again
[英]Hot Reloading Thymeleaf Templates and Resource Bundles in Spring Boot 2.1
我创建了一个启用了i18n的Spring Boot 2.1.3应用程序,添加了devtools,并安装了Firefox LiveReload扩展。 不幸的是,当我更改Thymeleaf模板或i18n消息时,它没有改变。 Spring Boot文档似乎建议您要做的就是安装devtools,它将禁用静态资源的缓存。
这是我创建应用程序所做的:
mkdir bootiful-i18n
cd bootiful-i18n
http https://start.spring.io/starter.zip dependencies==web,thymeleaf -d | tar xvz
然后我创建了HomeController.java
:
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@GetMapping("/")
String home() {
return "home";
}
}
我在src/main/resources/templates/home.html
创建了Thymeleaf模板:
<html xmlns:th="http://www.thymeleaf.org">
<body>
<h1 th:text="#{title}"></h1>
<p th:text="#{message}"></p>
</body>
</html>
我在src/main/resources
添加了messages.properties
文件:
title=Welcome
message=Hello! I hope you're having a great day.
这一切都很好。 为了启用热重载,我将devtools添加到pom.xml
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
并安装了Firefox的LiveReload扩展 。
我重新启动服务器,启用LiveReload并导航到http:// localhost:8080 。 我尝试更改并保存home.html
和messages.properties
并刷新浏览器。 浏览器中的输出不会更改。 在Spring Boot 2.1中,我还需要做些其他事情来禁用Thymeleaf模板和消息包的缓存吗?
Devtools将禁用缓存,以便在需要再次呈现模板文件时可以重新考虑对模板文件的更新。
但是,当您的应用程序在JVM进程中启动时,没有任何文件指向您的源文件,并且只能看到类路径。 因此,对源文件所做的任何更改都需要更新到类路径位置,这通常是通过要求IDE“构建项目”来进行的。
因此,模板缺少的步骤是,如果文档尚不明确,请随意提出一个建议,因为我们对此进行了很多迭代。
虽然没有关于i18n的具体信息。 如果您更改它们并更新类路径,我们将重新启动应用程序,而不是像对模板一样进行重新选择。 我们确实曾经尝试支持这一点, 有关更多详细信息 ,请参见此问题 。 话虽如此,您不应指望对资源文件的更改将“起作用”。 更改配置显然会要求应用程序重启(例如,再次使用devtools时通过更新类路径)。
有关IDE支持的一些补充说明:
如果您不使用IDE,则需要一些东西来为您更新类路径。 我们尝试开箱即用地支持它,但事实证明它相当复杂。 如果您使用Gradle,有一条评论可以帮助您 。
将以下内容添加到您的application.properties
:
spring.thymeleaf.cache=false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.