0

I have a website that was built using Spring Thymeleaf, and I'd like to generate the files, such as an index.html, css, and js files, in a flat directory so that I can serve the compiled/transpiled files from an AWS S3 bucket. The project is a Gradle project. How do I do this? Thank you in advance.

1 Answers1

2

You can run Thymeleaf as a Java program (rather than on a server) and generate HTML files like this:

public class Application {

  public static void main(String... args) {
    SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
    resolver.setApplicationContext(new AnnotationConfigApplicationContext());
    resolver.setPrefix("classpath:/html/");
    resolver.setSuffix(".html");
    resolver.setCharacterEncoding("UTF-8");
    resolver.setTemplateMode(TemplateMode.HTML);

    SpringTemplateEngine engine = new SpringTemplateEngine();
    engine.setTemplateResolver(resolver);

    Context context = new Context();
    String html = engine.process("index", context);
    System.out.println(html);
  }
}

Simply replace the System.out.println(html); with saving the html variable to a file. Then you just have to read your directory for all the files you want to interpret and run the code for everything.

This will not work for forms -- but I'm assuming you don't need any actually dynamic content if you are trying to compile your Thymeleaf to static html.

You will also have to include the right dependencies. I use these:

  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  
  <groupId>nz.net.ultraq.thymeleaf</groupId>
  <artifactId>thymeleaf-layout-dialect</artifactId>

  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  
  <groupId>org.thymeleaf</groupId>
  <artifactId>thymeleaf</artifactId>

  <groupId>org.thymeleaf</groupId>
  <artifactId>thymeleaf-spring5</artifactId>
Metroids
  • 18,999
  • 4
  • 41
  • 52