Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Juan Sedano
Juan Sedano

Posted on • Originally published atjsedano.dev on

Plain Thymeleaf

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 thisstackoverflow answer which in turn was found on thiscomment on github.

We begin by getting only the core jar for Thymeleaf (the latest one at the time this post was written).

pom.xml

<dependency>  <groupId>org.thymeleaf</groupId>  <artifactId>thymeleaf</artifactId>  <version>3.1.1.RELEASE</version></dependency>
Enter fullscreen modeExit fullscreen mode

Then we create ourtemplate inside resources on a directory apptly namedtemplates.

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>
Enter fullscreen modeExit fullscreen mode

Inside thespan tag we haveth 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 iffirst_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");
Enter fullscreen modeExit fullscreen mode

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]);  }}
Enter fullscreen modeExit fullscreen mode

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);
Enter fullscreen modeExit fullscreen mode

Here we create aTemplateEngine object and pass theresolver, then we callprocess on thetemplateEngine object with the name of the template and the context we created above.

We can create our fat jar runningmvn verify and then call it withjava -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.jarSLF4J: No SLF4J providers were found.SLF4J: Defaulting to no-operation (NOP) logger implementationSLF4J: 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 sedanoSLF4J: No SLF4J providers were found.SLF4J: Defaulting to no-operation (NOP) logger implementationSLF4J: 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>
Enter fullscreen modeExit fullscreen mode

For more information on Thymeleaf you can go directly to thethymeleaf docs.

Download the complete code for this here:only-thymeleaf.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Hi! I just want to learn and share : )
  • Location
    Guadalajara, México
  • Work
    Tech lead at Clip
  • Joined

More fromJuan Sedano

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp