liveBook · Manning

Thymeleaf

As explained in Spring Start Here, Second Edition

Thymeleaf is a Java server-side template engine for processing HTML and other markup templates with application data.

Thymeleaf is a template engine that lets a Spring controller send data to an HTML view for display. Add the Spring Boot Thymeleaf starter, then use a template such as home.html to show values that can vary between requests. section 8.1

A dynamic greeting

A controller adds username and color attributes to a Model and returns home.html. The template uses ${username} for the greeting and ${color} to set its style, so the page can display values supplied by the controller. section 8.1

Listing 8.2 The home.html file representing the dynamic view of the app
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">   #A
 
  <head>
    <meta charset="UTF-8">
    <title>Home Page</title>
  </head>
 
  <body>
    <h1>Welcome 
    <span th:style="'color:' + ${color}"      #B
          th:text="${username}"></span>!</h1> #B
  </body>
 
</html>

See the HTML template using Thymeleaf attributes to display the controller’s values. section 8.1

How Spring Start Here, Second Edition covers it

How deep it goes
Demonstrates a starter dependency, Model attributes, and template expressions in a small example.
Examples
Uses a username and color in a greeting, and a shopping cart whose displayed product list changes.
What you can do after
Build a dynamic HTML view by passing controller data through a Model and displaying it in a Thymeleaf template.
How it connects
Links Thymeleaf with Spring Boot, Spring MVC, controllers, Model attributes, and dynamic views.

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 Start Here, Second Edition. The section links point to the parts of the book it draws on.