5

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.

Mahozad
  • 18,032
  • 13
  • 118
  • 133
FC_Termi
  • 53
  • 1
  • 4
  • I don't understand what you're asking. Your main method can be wherever you want. – Michael Feb 13 '20 at 13:11
  • Ok, then how do I launch the webapp ? By instantiating inside the main method "GTVGApplication gtvgApplication = new GTVGApplication()" ? – FC_Termi Feb 13 '20 at 13:28

2 Answers2

3

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", "Lind");
        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)
}
Mahozad
  • 18,032
  • 13
  • 118
  • 133
0

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

mklepa
  • 211
  • 3
  • 7
  • OK, from what I understand after quickly reading the Github comment issues in the link you provided I should find a way to instantiate **GTVGApplication gtvgApplication = new GTVGApplication(servletContext);** in a main method. So I should first instantiate a local **ServletContext servletContext = new ServletContext();**, then I can use this parameter to instantiate gtvgApplication. After that everything will work ? – FC_Termi Feb 13 '20 at 13:44
  • Thymeleaf is for generating templates, you could think about this as populating HTML files with data, instead of typing it manually into the HTML yourself. Running a web app is a different story. For doing that I'd recommend using Spring MVC framework which integrates easily with Thymeleaf. If you want tutorials on how to integrate Thymeleaf with Spring MVC or Java EE, just google it, you will find plenty of good tutorials. If you don't belive me, just google "web app with thymeleaf", all the results will be about integrating Spring with Thymeleaf – mklepa Feb 13 '20 at 14:29
  • I know there are a lot of tutorials for Thymeleaf + Spring, but there is a job for which I will have to work with Thymeleaf without Spring. I never used Thymeleaf before so I'm kind of lost. Well, for now I will try to see how Thymeleaf + Spring works, then I will try without Spring if I can manage. – FC_Termi Feb 13 '20 at 14:47
  • I understand. I found [this tutorial](https://www.concretepage.com/thymeleaf/java-thymeleaf-example-getting-started-with-thymeleaf) that seems to suit your needs. In the future please include code as text, not in form of screenshots. You want someone to be able to reproduce your issue and it's a lot easier to just copy and paste the code than rewrite it from screenshots. Please see [this link](https://stackoverflow.com/help/minimal-reproducible-example) on how to create good questions. – mklepa Feb 13 '20 at 15:32
  • Thanks a lot man. This is the first time I'm making a post on StackOverflow. I didn't find the option to post "codes" the same way others do, so I ended up posting screenshots instead. Sorry for the inconvenience and thanks again. – FC_Termi Feb 13 '20 at 15:40
  • One more thing, I see this tutorial doesn't cover how to deploy an application to web server. In order to launch this application you need an application server, so you don't really use a main method. I found [this tutorial](https://dzone.com/articles/developing-java-ee-6) that will hopefully help you. I recommend that you read some articles about Java EE and application servers. – mklepa Feb 13 '20 at 15:57