liveBook · Manning

Thymeleaf

As explained in Spring in Action, Sixth Edition

Thymeleaf is a server-side Java template engine for generating dynamic HTML and other text-based views.

Thymeleaf is a server-side Java template engine that turns templates into views. In Spring MVC, a controller can return a logical view name, and Thymeleaf can render the matching HTML template. section 1.3.1 section 2.1.3

With the Web and Thymeleaf starters on the classpath, Spring Boot autoconfiguration sets up a Thymeleaf view resolver. A controller that returns home selects the template at src/main/resources/templates/home.html. section 1.3.6 section 1.3.1

Listing 1.4 The home page controller
package tacos;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
 
@Controller                     #1
public class HomeController {
 
  @GetMapping("/")              #2
  public String home() {
    return "home";              #3
  }
 
}

The home controller handles GET requests for / and returns the logical view name home. section 1.3.1

A template is HTML with Thymeleaf attributes that guide rendering. For example, th:src with an @{...} expression refers to a context-relative image path. The corresponding image belongs under src/main/resources/static. section 1.3.2

Listing 1.5 The Taco Cloud home page template
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
  <head>
    <title>Taco Cloud</title>
  </head>
  
  <body>
    <h1>Welcome to...</h1>
    <img th:src="@{/images/TacoCloud.png}"/>
  </body>
</html>

The home template uses th:src to refer to the Taco Cloud logo. section 1.3.2

To display controller data, Spring copies model values into servlet request attributes before handing the request to the view. A ${message} expression reads the message attribute, and th:text replaces an element’s body with its value. section 2.1.3

<h3>Designate your wrap:</h3>
<div th:each="ingredient : ${wrap}">
  <input th:field="*{ingredients}" type="checkbox" 
         th:value="${ingredient.id}"/>
  <span th:text="${ingredient.name}">INGREDIENT</span><br/>
</div>

The ingredient example uses th:each to repeat a block for each item in wrap. It also uses th:field, th:value, and th:text to create ingredient checkboxes and labels. section 2.1.3

Listing 2.5 The complete design-a-taco page
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
  <head>
    <title>Taco Cloud</title>
    <link rel="stylesheet" th:href="@{/styles.css}" />
  </head>
 
  <body>
    <h1>Design your taco!</h1>
    <img th:src="@{/images/TacoCloud.png}"/>
 
    <form method="POST" th:object="${taco}">
    <div class="grid">
      <div class="ingredient-group" id="wraps">
      <h3>Designate your wrap:</h3>
      <div th:each="ingredient : ${wrap}">
        <input th:field="*{ingredients}" type="checkbox" 
               th:value="${ingredient.id}"/>
        <span th:text="${ingredient.name}">INGREDIENT</span><br/>
      </div>
      </div>
 
      <div class="ingredient-group" id="proteins">
      <h3>Pick your protein:</h3>
      <div th:each="ingredient : ${protein}">
        <input th:field="*{ingredients}" type="checkbox" 
               th:value="${ingredient.id}"/>
        <span th:text="${ingredient.name}">INGREDIENT</span><br/>
      </div>
      </div>
 
      <div class="ingredient-group" id="cheeses">
      <h3>Choose your cheese:</h3>
      <div th:each="ingredient : ${cheese}">
        <input th:field="*{ingredients}" type="checkbox" 
               th:value="${ingredient.id}"/>
        <span th:text="${ingredient.name}">INGREDIENT</span><br/>
      </div>
      </div>
 
      <div class="ingredient-group" id="veggies">
      <h3>Determine your veggies:</h3>
      <div th:each="ingredient : ${veggies}">
        <input th:field="*{ingredients}" type="checkbox" 
               th:value="${ingredient.id}"/>
        <span th:text="${ingredient.name}">INGREDIENT</span><br/>
      </div>
      </div>
 
      <div class="ingredient-group" id="sauces">
      <h3>Select your sauce:</h3>
      <div th:each="ingredient : ${sauce}">
        <input th:field="*{ingredients}" type="checkbox" 
               th:value="${ingredient.id}"/>
        <span th:text="${ingredient.name}">INGREDIENT</span><br/>
      </div>
      </div>
      </div>
 
      <div>
 
 
      <h3>Name your taco creation:</h3>
      <input type="text" th:field="*{name}"/>
      <br/>
 
      <button>Submit Your Taco</button>
      </div>
    </form>
  </body>
</html>

The complete taco design template brings the image, stylesheet, form, and ingredient groups together. section 2.1.3

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains view resolution, model-to-request-attribute transfer, and template attributes through working examples.
Examples
Uses a home page with a logo and a taco design form with repeated ingredient checkboxes.
What you can do after
Apply Thymeleaf with Spring MVC to render controller-selected templates, display request data, and repeat form elements.
How it connects
Connects Thymeleaf with Spring MVC, Spring Boot autoconfiguration, servlet request attributes, and controller models.

Also explained in

Compare how 2 books explain Thymeleaf →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.