3

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

Neil
  • 153
  • 1
  • 9

2 Answers2

1

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:

xerx593
  • 12,237
  • 5
  • 33
  • 64
  • When I moved page1.html to the templates directory, I get a 404 error when I go to `http://dev.propfinancing.com/www/page1.html` What do I need to do to load it now? – Neil Jan 09 '22 at 16:34
  • I am trying myself (jar, embedded), please meanwhile inspect the stacktraces (sorry, i also produced 2-3 on `/www/templates/page1.html`, without `/templates/`, `.html` (brute force) ;) ..its also much in the "environment" and "other libs&config" details. – xerx593 Jan 09 '22 at 17:20
  • see update, bro – xerx593 Jan 09 '22 at 18:29
  • 1
    I tried to follow your first example of using @Controller (See my updated code above). When I go to `http://dev.propfinancing.com/www/page1.html`, I just get "page1", I don't get any of the templates. – Neil Jan 09 '22 at 21:03
  • also my bad: please replace the `SpringTemplateEngine` bean with `LayoutDialect` ([ref](https://ultraq.github.io/thymeleaf-layout-dialect/getting-started/)), because the latter uses (spring boot) auto configuration, whereas the first is the "DIY" approach (then we'd have to do the analogous to [src-link](https://github.com/spring-projects/spring-boot/blob/4256344c80a2dcb7de93383f47d08aca0522ab28/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java#L137) – xerx593 Jan 09 '22 at 21:11
  • 1
    That worked! I knew it had to be some simple things, but the resources I found on the web were not complete. Thank you! – Neil Jan 09 '22 at 21:57
0

In my case the problem was in the use of decorator I solved this issue by simply changing decorator to decorate

Here I had the error when I used decorator to configure the created template using Thymeleaf

<html xmlns:th="http://www.thymeleaf.org"
    xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
    layout:decorator="template1">

Then I changed the decorator keyword to decorate and it just worked fine :

<html xmlns:th="http://www.thymeleaf.org"
    xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
    layout:decorate="template1">
Alix
  • 78
  • 8
  • 1
    It's important to not just post code, but to also include a description of what the code does and why you are suggesting it. This helps others understand the context and purpose of the code, and makes it more useful for others who may be reading the question or answer, @Alix. – DSDmark Dec 21 '22 at 15:22