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:
- Variables
- Text
- Selected objects
- Links
- Literal substitutions
- 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 theirtoString()method invoked.
Methods
We can also call methods. For example:
<div ...