3

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?

2 Answers 2

6

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.

2
  • 1
    Thanks for the tips Stephane. I've found that spring.thymeleaf.cache and spring.thymeleaf.prefix are both necessary to get hot-reloading of Thymeleaf templates. For i18n, a build or "update on frame deactivation" (both which cause an app reload) works. Thanks! Commented Feb 25, 2019 at 14:32
  • Correction, spring.thymeleaf.prefix is needed if you want hot-reloading when you're not using an IDE and something like vi. spring.thymeleaf.cache doesn't seem to be necessary. Commented Feb 25, 2019 at 14:58
1

Add the following to your application.properties :

spring.thymeleaf.cache=false
2
  • I recently created a new project, parent: spring-boot-starter-parent, dependencies: spring-boot-starter-tomcat, spring-boot-starter-thymeleaf, and templates are refreshed when I edit them. Although I didn't try properties.
    – Selindek
    Commented Feb 24, 2019 at 19:13
  • I had to add both spring.thymeleaf.cache and spring.thymeleaf.prefix for hot-reloading to work. FWIW, I was using Vi when I first tried to get things working. Maybe you're using Eclipse or something that auto-updates the target directory when you change files? Commented Feb 25, 2019 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.