19

When I change a thymeleaf .html file residing inside /templates , I expect the browser to automatically reload the page. I have the live reload plugin installed and its able to do a handshake with the spring boot server. However , upon chaning a thymeleaf template file , I have to manually reload the browser. Any suggestions what I might be missing. Obviously I have spring-boot-devtools enabled and have also manually updated the property devtools.livereload.enabled = true. And spring devtools is correctly relecting changes to any template or controller in build target , and with a manual reload of browser , I see the changes.

As per spring docs.

Certain resources do not necessarily need to trigger a restart when they are changed. For example, Thymeleaf templates can be edited in-place. By default, changing resources in /META-INF/maven, /META-INF/resources, /resources, /static, /public, or /templates does not trigger a restart but does trigger a live reload.

I have my local running over a broken https. ( Some certificate issue , which causes a not secure message in chrome address bar. Could this be the reason live reload is not working.

Rpant
  • 974
  • 2
  • 14
  • 37
  • Check out https://reflectoring.io/spring-boot-dev-tools/ and https://www.wimdeblauwe.com/blog/2019/2019-10-20-spring-boot-and-thymeleaf-with-css-javascript-processing-using-gulp/ – Wim Deblauwe Aug 21 '20 at 12:53

4 Answers4

30

After 3 years of frustation here is a solution for a working HOT SWAP:

https://attacomsian.com/blog/spring-boot-auto-reload-thymeleaf-templates

Below are my tested settings (this is the only thing I've changed for it to work).

application.yaml:

spring:
  thymeleaf: # Thymeleaf
    cache: false
    mode: HTML
    encoding: UTF-8
    prefix: file:src/main/resources/templates/
  resources: # Static resources
    static-locations: file:src/main/resources/static/
    cache:
      period: 0

What it does:

  • Applies changes to your views (and/or static content) if you've altered anything in static/ or templates/
  • It does so without the need to manually compile or reboot the entire web server

What it does not:

  • Reload the page for you in the browser (you still have to refresh the page in the browser; btw that LiveReload Chrome extension won't work either, maybe with some webpack magic you can make it work)

This is all assuming you didn't override the default paths (/resources/templates, /resources/static).

P.S. I've tried with a self-signed TLS cert, it still works. Yes it won't behave like Angular with browser page reloading out of the box.

downvoteit
  • 588
  • 2
  • 7
  • 12
  • 3
    For me it was enough to set `spring.thymeleaf.cache=false`. Also you may want to put this into an `application-development.yml` to avoid having this in a production context (see: https://stackoverflow.com/questions/39921174/using-two-yaml-files-for-configuration-properties) – lanoxx Jun 07 '22 at 10:25
  • 1
    @lanoxx, fair point. This should not end up in prod. But to be honest `prefix: file: ..` will not resolve correctly inside a jar/war anyway. – downvoteit Jun 16 '22 at 07:19
  • 1
    This is great @ downvoteit. I have been frustrated by this for a long time too. Never thought it'd be so easy. Thanks @lanoxx too for your comment. – GerardV Dec 30 '22 at 14:24
  • For me the static resources wouldn't reload until I changed `resources` to `web.resources` in my properties file (looks like it was renamed) – szx Mar 01 '23 at 07:41
13

To have HTML / CSS / JS reloading automatically in Spring Thymeleaf can be simple and bug free, have only tested in IntelliJ.

Add this to maven, use ${spring.version} var or replace by your version:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <version>${spring.version}</version>
    <optional>true</optional>
    <scope>runtime</scope>
</dependency>

Add to the header of the html:

<script src="http://localhost:35729/livereload.js"></script>

When using IntelliJ:

using Ctrl+Shift+A write Registry

In IntelliJ Settings

Apóstolo
  • 156
  • 1
  • 5
  • 2
    This is the correct answer for this question. – divinedragon Jun 17 '21 at 20:29
  • I was just trying to to this again and fortunately I found this eheheh – Apóstolo Nov 05 '22 at 20:32
  • This actually works in Spring Boot (2.7.11) with spring-boot-devtools as dependency. Make a change to your page, save and watch the page update. Just like that. No need to put an actual js file in resources. – MiB May 14 '23 at 04:40
0

I had the same issue. I had spring boot dev tools and templates in /src/main/webapp/WEB-INF/templates. The only thing i needed to do was set spring.thymeleaf.cache to false. In my case i had to do it in the java code.

@Bean
public SpringResourceTemplateResolver templateResolver(){
    // SpringResourceTemplateResolver automatically integrates with Spring's own
    // resource resolution infrastructure, which is highly recommended.
    final SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
    templateResolver.setApplicationContext(this.applicationContext);
    templateResolver.setPrefix("/WEB-INF/templates/");
    templateResolver.setSuffix(".html");
    // HTML is the default value, added here for the sake of clarity.
    templateResolver.setTemplateMode(TemplateMode.HTML);
    // Template cache is true by default. Set to false if you want
    // templates to be automatically updated when modified.
    templateResolver.setCacheable(false);
    templateResolver.setOrder(2);
    return templateResolver;
}

I got this sample code from thymeleaf https://github.com/thymeleaf/thymeleafexamples-springmail/blob/3.0-master/src/main/java/thymeleafexamples/springmail/business/SpringMailConfig.java

  • Actually, I believe spring-boot-devtools makes setting this unnecessary. i e it automatically disables all template caching. – MiB May 14 '23 at 04:42
-1

EDIT:

You can enbale automatic regreshing of your browser with e.g. a live reload plugin. You could use http://livereload.com/extensions/

A hint from official spring boot docs is here: https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html#using-boot-devtools-livereload

Explanation why it does not work out of the box

I think you are mixing things here. "Live reload" in terms of Spring Boot applications means that you do not need to restart your application server after editing your html. This does not mean that you get hot reloading like in angular from webpack dev server.

So you still have to trigger a refresh of your browsers page.

See here for more details for how it works in WDS: https://webpack.js.org/concepts/hot-module-replacement/

And here from spring's perspective https://docs.spring.io/spring-boot/docs/current/reference/html/howto-hotswapping.html

mrkernelpanic
  • 4,268
  • 4
  • 28
  • 52
  • Thats not true , I have added a hyperlink in the quotation in my question to make clear , what the spring team means by 'live reload' , just like it is in original spring docs. You should be able to see the contradiction in your own explanation here , or just follow your second link. Problem with this over hyped `just works` , `ENTERPRISE` framework is that you have to spend hours trying to make it just work. facebook can write facebook in php and google doesn't use `enterprise` solutions either , Any other stack , rails , play , is more `just works` than this dinosaur. – Rpant Oct 08 '19 at 17:41
  • And have you tried with the browser plugin? Did it work out? BTW although spring framework is a universe for its own, life got substantially easier with spring boot and auto configuration. – mrkernelpanic Oct 10 '19 at 14:33
  • Yes I mentioned in the question that the browser plugin does a successful handshake with the live reload server . I don't care raising a bug on their github because it would get closed like the 17000 other bugs that they have closed. No respectable open source project has 17000 bugs on their github. Made life easy ? Maybe if its still year 2010. – Rpant Oct 11 '19 at 16:01