'How to use Thymeleaf to make only a simple Java app (without Spring)

I'm following the official Thymeleaf tutorial on its website and I'm currently on the section Executing the template engine.

From what I understood, I should already be able to run the app coded so far, but I absolutely don't see how to run it. There is no main(String[] args) method to run at all.

I tried searching for other tutorials but they all use Spring which is not what I'm looking for right now.

Anyone knows where I should insert a main(String[] args) method to run this Thymeleaf app and view my HTML template? I don't understand where the entry point is or should be.

I apologize in advance if this question sounds dumb and thanks for your future replies.


Edit:

Until now, when following the tutorial I wrote 3 Java files:

So I thought of writing a main method like so: Main class containing main(String[] args) method

But I don't see how to correctly instantiate the servletContext and I'm not even sure if everything will work out once this is done.



Solution 1:[1]

You can create your main method in any class that you created and then create Thymeleaf objects that you need. This might help

Solution 2:[2]

Borrowing from this comment on GitHub, here is how to use Thymeleaf in a simple app.

First, add the Thymeleaf library (dependency):

  • If you are using Maven, add this to your pom.xml file:
    <dependencies>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
            <version>3.0.15.RELEASE</version>
        </dependency>
        <!-- Add this if you don't like seeing messages in stdout from SLF4J -->
        <!-- 
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-nop</artifactId>
            <version>1.7.36</version>
        </dependency> 
        -->
    </dependencies>
    
  • If you are using Gradle, add this to your build.gradle[.kts] file:
    dependencies {
        implementation("org.thymeleaf:thymeleaf:3.0.15.RELEASE")
        // Add this if you don't like seeing messages in stdout from SLF4J
        // implementation("org.slf4j:slf4j-nop:1.7.36")
    }
    

Place your template HTML files in templates/ subdirectory of your classpath (for example, in src/main/resources/templates/).

src/main/resources/templates/index.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>Sample Thymeleaf template</title>
</head>
<body>

<p th:text="|Hello, ${name}!|">Hello, user!</p>
<p th:text="|Today is ${date}.|">Today is ...</p>

</body>
</html>

And here is the program code if you are using Java language:

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import java.time.LocalDateTime;

public class Main {

    public static void main(String[] args) {
        // From Java 10, you can use var instead of declaring the type explicitly
        var resolver = new ClassLoaderTemplateResolver();
        resolver.setTemplateMode(TemplateMode.HTML);
        resolver.setCharacterEncoding("UTF-8");
        resolver.setPrefix("/templates/");
        resolver.setSuffix(".html");

        var context = new Context();
        context.setVariable("name", "Land");
        context.setVariable("date", LocalDateTime.now().toString());

        var templateEngine = new TemplateEngine();
        templateEngine.setTemplateResolver(resolver);

        var result = templateEngine.process("index", context);
        System.out.println(result);
    }
}

And here is the program code if you are using Kotlin language:

import org.thymeleaf.TemplateEngine
import org.thymeleaf.context.Context
import org.thymeleaf.templatemode.TemplateMode
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver
import java.time.LocalDateTime

fun main() {
    val resolver = ClassLoaderTemplateResolver().apply {
        templateMode = TemplateMode.HTML
        characterEncoding = "UTF-8"
        prefix = "/templates/"
        suffix = ".html"
    }
    val context = Context().apply {
        setVariable("name", "Lind")
        setVariable("date", LocalDateTime.now().toString())
    }
    val templateEngine = TemplateEngine().apply {
        setTemplateResolver(resolver)
    }
    val result = templateEngine.process("index", context)
    println(result)
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mklepa
Solution 2