简体   繁体   中英

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. Unfortunately, when I change my Thymeleaf templates or i18n messages, it doesn't change. The Spring Boot documentation seems to suggest all you need to do is install devtools, and it'll disable caching of static resources.

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 :

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 :

<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 :

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 :

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

And installed the LiveReload extension for Firefox .

I restarted my server, enabled LiveReload and navigated to http://localhost:8080 . I tried changing and saving both home.html and messages.properties and refreshing my browser. 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?

Devtools will disable caching so that an update to the template file can be reconsidered when it needs to be rendered again.

When your application starts in a JVM process though, there is nothing pointing to your source files and it can only see the classpath. 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".

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. 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).

Some added note about IDEs support:

  • Eclipse incrementally compiles and copies changed files on "save". 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. 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

If you're not using an IDE, you need to have something to update the classpath for you. 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 .

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

spring.thymeleaf.cache=false

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