简体   繁体   English

Spring Boot 2.1中的热重装Thymeleaf模板和资源包

[英]Hot Reloading Thymeleaf Templates and Resource Bundles in Spring Boot 2.1

I created a Spring Boot 2.1.3 application with i18n enabled, added devtools, and installed the Firefox LiveReload extension. 我创建了一个启用了i18n的Spring Boot 2.1.3应用程序,添加了devtools,并安装了Firefox LiveReload扩展。 Unfortunately, when I change my Thymeleaf templates or i18n messages, it doesn't change. 不幸的是,当我更改Thymeleaf模板或i18n消息时,它没有改变。 The Spring Boot documentation seems to suggest all you need to do is install devtools, and it'll disable caching of static resources. Spring Boot文档似乎建议您要做的就是安装devtools,它将禁用静态资源的缓存。

Here's what I did to create my app: 这是我创建应用程序所做的:

mkdir bootiful-i18n
cd bootiful-i18n
http https://start.spring.io/starter.zip dependencies==web,thymeleaf -d | tar xvz

Then I created a HomeController.java : 然后我创建了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";
   }
}

I created a Thymeleaf template at src/main/resources/templates/home.html : 我在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>

I added a messages.properties file in src/main/resources : 我在src/main/resources添加了messages.properties文件:

title=Welcome
message=Hello! I hope you're having a great day.

This all works great. 这一切都很好。 To enable hot-reload, I added devtools to pom.xml : 为了启用热重载,我将devtools添加到pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

And installed the LiveReload extension for Firefox . 并安装了FirefoxLiveReload扩展

I restarted my server, enabled LiveReload and navigated to http://localhost:8080 . 我重新启动服务器,启用LiveReload并导航到http:// localhost:8080 I tried changing and saving both home.html and messages.properties and refreshing my browser. 我尝试更改并保存home.htmlmessages.properties并刷新浏览器。 The output in the browser does not change. 浏览器中的输出不会更改。 Is there something else I need to do to disable caching of Thymeleaf templates and message bundles in Spring Boot 2.1? 在Spring Boot 2.1中,我还需要做些其他事情来禁用Thymeleaf模板和消息包的缓存吗?

Devtools will disable caching so that an update to the template file can be reconsidered when it needs to be rendered again. Devtools将禁用缓存,以便在需要再次呈现模板文件时可以重新考虑对模板文件的更新。

When your application starts in a JVM process though, there is nothing pointing to your source files and it can only see the classpath. 但是,当您的应用程序在JVM进程中启动时,没有任何文件指向您的源文件,并且只能看到类路径。 So whatever change you make to a source file needs to be updated to the classpath location, which usually happens by asking your IDE to "Build the project". 因此,对源文件所做的任何更改都需要更新到类路径位置,这通常是通过要求IDE“构建项目”来进行的。

So the missing step for the template is that and if the documentation is not clear about that, feel free to open an issue with a suggestion as we've been iterating on that quite a lot. 因此,模板缺少的步骤是,如果文档尚不明确,请随意提出一个建议,因为我们对此进行了很多迭代。

There is nothing specific about i18n though. 虽然没有关于i18n的具体信息。 If you change them and update the classpath we'll restart the app rather than picking this up the same way we do for templates. 如果您更改它们并更新类路径,我们将重新启动应用程序,而不是像对模板一样进行重新选择。 We did try to support this once, see this issue for more details . 我们确实曾经尝试支持这一点, 有关更多详细信息 ,请参见此问题 Having said that, you shouldn't expect that a change to a resource file "will just work". 话虽如此,您不应指望对资源文件的更改将“起作用”。 A change to configuration will obviously require the app to restart for instance (again, by updating the classpath when you use devtools). 更改配置显然会要求应用程序重启(例如,再次使用devtools时通过更新类路径)。

Some added note about IDEs support: 有关IDE支持的一些补充说明:

  • Eclipse incrementally compiles and copies changed files on "save". Eclipse以增量方式编译并复制“保存”中的更改文件。 So if you save your template you should be good to go 因此,如果您保存模板,应该会很好
  • IntelliJ IDEA Ultimate has dedicated devtools support as of 2018.1 - If you configure "On frame deactivation" to "Update resources" it will do the right thing when you switch to your browser. IntelliJ IDEA Ultimate从2018.1开始具有专用的devtools支持 -如果将“停用框架”配置为“更新资源”,则在切换到浏览器时将做正确的事情。 Before that support I had a special keystroke mapped to "Build Project" (incrementally compiles and copies resources) that I invoked whenever I wanted my changes to be reflected 在获得该支持之前,我有一个特殊的按键映射到“ Build Project”(递增地编译和复制资源),每当我希望反映所做的更改时便调用该按键

If you're not using an IDE, you need to have something to update the classpath for you. 如果您不使用IDE,则需要一些东西来为您更新类路径。 We've tried to support that out-of-the-box but turned out to be quite complex. 我们尝试开箱即用地支持它,但事实证明它相当复杂。 There is a comment to help you if you're using Gradle . 如果您使用Gradle,一条评论可以帮助您

将以下内容添加到您的application.properties

spring.thymeleaf.cache=false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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