Thymeleaf
is an HTML (and more) template engine for Java. This is the core in action with nothing else.
You can find the complete code for this here: only-thymeleaf.
All of this is just putting into a project what has being posted in this stackoverflow answer which in turn was found on this comment on github.
We begin by getting only the core jar for Thymeleaf (the latest one at the time this post was written).
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
Then we create our template
inside resources on a directory apptly named templates
.
src/main/resources/templates/index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Thymeleaf hello world</title>
</head>
<body>
<p>First name: <span th:text="${first_name}?: '(no first name specified)'"/>.</p>
<p>Last name: <span th:text="${last_name}?: '(no last name specified)'"/>.</p>
</body>
</html>
Inside the span tag
we have th
which is how all data attributes starts on Thymeleaf, you can see that it’s declared as a namespace definition above on the html tag. We are also using a variable expression ${}
with an Elvis operator ?:
which is an special operator that only returns it’s value when the evaluated expression returns null, which means if first_name
is null then (no first name specified)
will be returned.
Now to bring it all together:src/main/java/dev/jsedano/examples/onlythymeleaf/Main.java
var resolver = new ClassLoaderTemplateResolver();
resolver.setTemplateMode(TemplateMode.HTML);
resolver.setCharacterEncoding("UTF-8");
resolver.setPrefix("/templates/");
resolver.setSuffix(".html");
Since we are not using Spring or nothing to helps us with dependency injection we will create objects the traditional way, setting up the prefix for where the templates will be stored and the suffix for them.
var context = new Context();
if (args.length != 0) {
context.setVariable("first_name", args[0]);
if (args.length > 1) {
context.setVariable("last_name", args[1]);
}
}
This will be the context which will hold the objects that Thymeleaf will use to generate the html.
var templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(resolver);
var result = templateEngine.process("index", context);
System.out.println(result);
Here we create a TemplateEngine
object and pass the resolver
, then we call process
on the templateEngine
object with the name of the template and the context we created above.
We can create our fat jar running mvn verify
and then call it with java -jar ./target/only-thymeleaf-1.0-jar-with-dependencies.jar
to print the result of the template processing, if we send variables when we executed we can see how they get replace:
only-thymeleaf % java -jar ./target/only-thyme leaf-1.0-jar-with-dependencies.jar
SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See https://www.slf4j.org/codes.html#noProviders for further details.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Thymeleaf hello world</title>
</head>
<body>
<p>First name: <span>(no first name specified)</span>.</p>
<p>Last name: <span>(no last name specified)</span>.</p>
</body>
</html>
only-thymeleaf % java -jar ./target/only-thymeleaf-1.0-jar-with-dependencies.jar juan sedano
SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See https://www.slf4j.org/codes.html#noProviders for further details.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Thymeleaf hello world</title>
</head>
<body>
<p>First name: <span>juan</span>.</p>
<p>Last name: <span>sedano</span>.</p>
</body>
</html>
For more information on Thymeleaf you can go directly to the thymeleaf docs.
Download the complete code for this here: only-thymeleaf.
Top comments (0)