question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using thymeleaf without Spring, Servlet Container, Controller specific logic

See original GitHub issue

I have missed about 30 minutes trying to figure out how to generate email HTML body from my service. This is a scheduled task, not an API call - means no controllers and Spring integrations. I have raw java and I want to process single *.html file with Thymeleaf. How to do that?

In other words, I need Thymeleaf analogy for Velocity example:

VelocityEngine ve = new VelocityEngine();
ve.init();
Template t = ve.getTemplate( "helloworld.vm" );
VelocityContext context = new VelocityContext();
context.put("name", "World");
StringWriter writer = new StringWriter();
t.merge( context, writer );

P.S. I’ve read this issue, it doesn’t provide an answer. Both Thymeleaf doc and thymeleafexamples-gtvg are bound to controller logic, resolvers and other stuff I do not need.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:9
  • Comments:18 (7 by maintainers)

github_iconTop GitHub Comments

28reactions
huffstlercommented, May 8, 2019

For those that may find this later on (like I did), I have a small working example.

pom dependencies

<dependencies>
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf</artifactId>
        <version>3.0.11.RELEASE</version>
    </dependency>
    <!-- 
    If you don't like seeing messages in stdout from slf4j, add this dependency 
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-nop</artifactId>
        <version>1.7.25</version>
    </dependency> 
    -->
</dependencies>

resources/templates/greeting.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Name & Date HTML example</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p th:text="'Hello, ' + ${name} + '!'" />
    <p th:text="'Today is ' + ${date} + '.'"/>
</body>
</html>

Application.java in hello package

package hello;

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 Application {
    public static void main(String[] args) {
        TemplateEngine templateEngine = new TemplateEngine();
        ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
        resolver.setPrefix("/templates/");
        resolver.setSuffix(".html");
        resolver.setCharacterEncoding("UTF-8");
        resolver.setTemplateMode(TemplateMode.HTML); // HTML5 option was deprecated in 3.0.0
        templateEngine.setTemplateResolver(resolver);
        Context ct = new Context();
        ct.setVariable("name", "foo");
        ct.setVariable("date", LocalDateTime.now().toString());
        System.out.println(templateEngine.process("greeting.html", ct));
    }
}
2reactions
mahozadcommented, Apr 25, 2022

Here is a related Stack Overflow post: How to use Thymeleaf to make only a simple Java app (without Spring).

Borrowing from @huffstler solution above for Java, here is the same solution for Kotlin:

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("greeting", context)
    println(result)
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Tutorial: Using Thymeleaf
The main goal of Thymeleaf is to provide an elegant and highly-maintainable way of creating templates. To achieve this, it builds on the...
Read more >
Spring MVC Data and Thymeleaf - Baeldung
In this tutorial, we'll talk about different ways to access Spring MVC data using Thymeleaf. We'll start by creating an email template using...
Read more >
B Spring MVC and Thymeleaf Template Engine
If you are not familiar with Spring MVC, refer to the Spring MVC ... Besides, the controller also returns a logical view name...
Read more >
Web on Servlet Stack - Spring
The DispatcherServlet , as any Servlet , needs to be declared and mapped according to the Servlet specification by using Java configuration or...
Read more >
Spring Boot Thymeleaf View - Javatpoint
Note: Do not forget to implement the following in the template file. · package com.javatpoint; · import org.springframework.boot.SpringApplication; · import org.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found