Thymeleaf th:object and Asterisk Syntax *{ }
1. th:object and Asterisk syntax
No ADS
In Thymeleaf , a variable expression has a ${ } syntax. In addition, *{ } is also a variable expression used quite often, In this lesson we are going to learn about it.
The asterisk syntax evaluates expressions on selected objects rather than on the whole context. To select an object you use the th:object attribute. See a simple example below:
(Asterisk syntax)
<div th:object = "${person}" class="box">
<p><b>Full Name:</b> <span th:utext="*{fullName}"></span></p>
<p><b>Email:</b> <span th:utext="*{email}"></span></p>
</div>
It is exactly equivalent to:
(Dollar syntax)
<div class="box">
<p><b>Full Name:</b> <span th:utext="${person.fullName}"></span></p>
<p><b>Email:</b> <span th:utext="${person.email}"></span></p>
</div>
And of course you can mix the Asterisk syntax and dollar syntax , for example:
(Mix syntax)
<div th:object = "${person}" class="box">
<p><b>Full Name:</b> <span th:utext="*{fullName}"></span></p>
<p><b>Email:</b> <span th:utext="${person.email}"></span></p>
</div>
What happens when you do not select any object, but use the asterisk *{ } syntax, In which case ${ } and *{ } work the same.
<div class="box">
<p><b>Full Name:</b> <span th:utext="*{person.fullName}"></span></p>
<p><b>Email:</b> <span th:utext="*{person.email}"></span></p>
</div>
<!--/* Same as: */-->
<div class="box">
<p><b>Full Name:</b> <span th:utext="${person.fullName}"></span></p>
<p><b>Email:</b> <span th:utext="${person.email}"></span></p>
</div>
2. Spring Boot Form
In Spring Boot, when handling Form you very often meet Asterisk Syntax:
(Form)
<form th:action="@{/register}" th:object="${appUserForm}" method="POST">
User Name:
<input type="text" th:field="*{userName}" />
<br/> Password:
<input type="password" th:field="*{password}" />
<br/> Confirm:
<input type="password" th:field="*{confirmPassword}" />
<br/> Email:
<input type="text" th:field="*{email}" />
<br/>
<input type="submit" value="Submit" />
</form>
No ADS
Thymeleaf Tutorials
- Thymeleaf Elvis Operator Tutorial with Examples
- Thymeleaf Loops Tutorial with Examples
- Thymeleaf Conditional statements: if, unless, switch
- Thymeleaf Predefined Objects Tutorial with Examples
- Thymeleaf th:class, th:classappend, th:style, th:styleappend
- Introduction to Thymeleaf
- Thymeleaf Variables Tutorial with Examples
- Thymeleaf Fragments Tutorial with Examples
- Thymeleaf Page Layouts Tutorial with Examples
- Thymeleaf th:object and Asterisk Syntax *{ }
- Thymeleaf Form Select option Example
Show More