New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using thymeleaf without Spring, Servlet Container, Controller specific logic #561
Comments
|
You've basically got the solution already, but maybe a few more looks to the Javadocs could have helped you out for the processing methods that you needed. Anyway, I've created standalone Thymeleaf template engines several times, mostly for writing tests, but the only code I can give you a link/reference to is a Groovy project which creates a Thymeleaf engine and then uses it to process an XML file. Here's the creation of the template engine: https://github.com/ultraq/rss-xml-generator/blob/master/Source/nz/net/ultraq/rss/RssXmlGenerator.groovy#L42-L53 That can probably be made into Java for your HTML use case like so: TemplateEngine templateEngine = new TemplateEngine();
TemplateResolver templateResolver = new TemplateResolver();
templateResolver.setTemplateMode('HTML');
templateEngine.setTemplateResolver(templateResolver);Then the processing happens in a method just below: https://github.com/ultraq/rss-xml-generator/blob/master/Source/nz/net/ultraq/rss/RssXmlGenerator.groovy#L64-L69 Again, as Java: Context context = new Context();
context.setVariable("name", "World");
StringWriter stringWriter = new StringWriter();
templateEngine.process("HelloWorld.html", context, stringWriter);
|
|
@ultraq I get the next exception at runtime:
|
|
Whoops, looks like I had an error in my example when translating from Groovy to Java. The |
|
@ultraq I've replaced it with |
|
The type declaration also needs to be changed - there is no |
|
@ultraq in my pom.xml I use But should I include any other dependencies? |
|
That dependency is Spring-specific, and given the title of this issue I don't think you want to use Spring. So you might want to change that to just the Thymeleaf core library only, which just has the artifactId of |
|
@ultraq I've changed my pom.xml dependency to: It still throws exception at runtime when I try to invoke
My full version of code: |
|
Could you double check that the Thymeleaf JAR is on the classpath when executing? I copied your code above and made a basic main method that called your method with "HelloWorld.html" (I also made that template) and output it to the console and it worked if I managed to get the This would get an exception: This wouldn't: (Note: I'm on a Mac, so your path separator symbol may vary) |
|
How are you running your program? I was using the plain old I don't know how to use Maven to run a program however - I only know how to use it to retrieve dependencies. If there is something like a Just to verify, I also unzipped the |
|
@ultraq I checked my classpath, thymeleaf jar is there. I unpacked it and check - everything is there. Haven't any clue at this point, but that's look really as maven packaging issue. Thank you for your time and patience! |
|
Not a problem @Volodymyr128. Just out of curiosity I googled "maven classnotfoundexception" and found a few StackOverflow posts which talk about making sure the |
|
@ultraq that appears that classpath issues were the problems of Intellij cahce. Once I overcome them I get
Just to complete this issue, you should insert into example above |
|
For those that may find this later on (like I did), I have a small working example.
<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>
<!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>
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));
}
} |
|
For me it worked after including attoparser,javassist,log4j,ognl,slf4j,thymeleaf and unbescape libraries. |
|
@huffstler 's example should be included in the examples list |
|
I made a example maven project for myself. |
|
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)
} |
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:
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.
The text was updated successfully, but these errors were encountered: