Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to use Thymeleaf in Java

Aymen Bashir

Thymeleaf is a server-side template engine for Java that is meant to bridge the gap between designers and developers. Thymeleaf allows the development of templates in a designer-friendly manner by minimizing the involvement of template understanding in prototyping design components. Therefore, it allows convenient integration of server-side and client-side frameworks.

Thymeleaf allows the processing of HTML, XML, JavaScript, CSS, and text. It is commonly used to generate HTML views for web apps.

Thymeleaf template

A Thymeleaf template can be an HTML page with Thymeleaf expressions. It may also include dynamic content for Thymeleaf expressions.

Let’s look at a simple HTML template in Thymeleaf below:

      <th th:text="#{}">Name</th>
      <th th:text="#{msgs.headers.price}">Price</th>
    <tr th:each="prod: ${allProducts}">
      <td th:text="${}">Oranges</td>
      <td th:text="${#numbers.formatDecimal(prod.price, 1, 2)}">0.99</td>

The Thymeleaf template is marked with th. Thymeleaf is processed on the server.

Thymeleaf engine

The Thymeleaf engine parses the Thymeleaf templates and replaces the template marked positions with Java model data to create new text on an HTML page.

The illustration above shows the Thymeleaf template engine working. The Thymeleaf engine parses the Thymeleaf template and replaces the template with the names in the Java model. The th:each template is used for iteration over the list of persons.




Aymen Bashir
Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring