Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf not loading template

I am using Spring and Thymeleaf. Thanks to xerx593, I was able to get it working so I updated this question to show the working code.

Here is my application class

package com.propfinancing.www;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import nz.net.ultraq.thymeleaf.layoutdialect.LayoutDialect;

@Controller
@SpringBootApplication
public class PfWebApplication extends SpringBootServletInitializer {

  public static void main(String[] args) {
    SpringApplication.run(PfWebApplication.class, args);
  }
  
  @Bean
  public LayoutDialect layoutDialect() {
    return new LayoutDialect();
  }

  @GetMapping("/page1.html")
  public String page1() {
    return "page1";
  }
}

Next, I create a layout.html file in src/main/resources/templates/layout.html

<!DOCTYPE html>
<html lang="en" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">

<body>
    This is the layout template
    <div layout:fragment="content">
        <p>This is were the content will go</p>
    </div>
</body>

</html>

And fin ally, I created /ser/main/resources/templates/page1.html to use the template:

<!DOCTYPE html>
<html lang="en" 
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="~{layout}">
      
<body>      
  <div layout:fragment="content">
    This is the content of page 1.
  </div>
</body>

</html>      

When I go to http://dev.propfinancing.com/www/page1.html, it gives me the template driven output I was expecting.

Thanks! Neil

like image 545
Neil Avatar asked Jun 22 '26 04:06

Neil


1 Answers

The most obvious mistake:

  1. I created a simple html page called page1.html in my src/main/resources/static directory

    (This is super for (spring-web) static content, but...)

  2. And finally, I updated my page1.html to use the template...

Updating is not enough, you have to also move it to a configured template location! So moving the file to src/main/resources/templates/ (default location, issuing same browser request,) will hopefully/probably produce the desired result(, or at least throw an exception).


In short: src/main/resources/static directory is not intended for templates! (It can still be configured, but this would be very strange/hacky/bunch full of "side effects"!?).


Ok, the 404, can be fixed (simply) with:

@Controller // !
@SpringBootApplication
public class ThymeleafTestApplication {

  public static void main(String[] args) {
    SpringApplication.run(ThymeleafTestApplication.class, args);
  }

  @GetMapping("/page1.html") // !!
  public String page1() {
    return "page1"; // returning the view name
  }
// ...
}

i.e. by providing a "controller" for this "view".

Or by configuring:

@SpringBootApplication
public class PfWebApplication // extends ... 
   implements WebMvcConfigurer {

  @Override
  public void addViewControllers (ViewControllerRegistry registry) {
      ViewControllerRegistration r = registry.addViewController("/page1.html");
      r.setViewName("page1");
      // r.set...
  }
...

One important thing is, that:

  @Bean
  public LayoutDialect layoutDialect() { 
    return new LayoutDialect();
  }

is the "auto-configuration" approach, which equips us with "all the spring (boot) magic".

Whereas:

  @Bean
  public SpringTemplateEngine templateEngine() {
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.addDialect(new LayoutDialect());
    return templateEngine;
  }

..is the "DIY" approach, and we'd have to tune (like e.g. spring-boot does).

Links:

  • https://ultraq.github.io/thymeleaf-layout-dialect/
  • https://docs.spring.io/spring-boot/docs/current/reference/html/web.html#web
  • https://www.thymeleaf.org/doc/articles/layouts.html
like image 190
xerx593 Avatar answered Jun 26 '26 08:06

xerx593



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!