Search⌘ K
AI Features

Thymeleaf Expressions

Explore the various Thymeleaf expressions that enable dynamic content rendering in Spring Boot applications. Learn to reference variables, call methods, access properties, and construct URLs with special syntax. Understand how to use expression inlining and literal substitutions to create flexible and localized templates.

We just introduced the Thymeleaf expressions (enclosed within th:* tags). These can be classified into six categories:

  1. Variables
  2. Text
  3. Selected objects
  4. Links
  5. Literal substitutions
  6. Expression inlining

Variables

When a Thymeleaf template gets processed, the application will put variables in the context via the controller. We can reference those variables in the templates via the ${…} syntax. For example:

<div th:text="${username}"></div>

Suppose there is a String in the Thymeleaf context with the name username that has the value “John Doe.” The HTML will be rendered as:

<div>John Doe</div>

The variable in the context does not need to be a String. Other types will have their toString() method invoked.

Methods

We can also call methods. For example:

<div
...