Thymeleaf Expressions
Get introduced to the usage of Thymeleaf expressions.
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 th:text="${user.getName()}&q
...