mydomain
No ADS
No ADS

Thymeleaf th:object and Asterisk Syntax *{ }

  1. th:object and Asterisk syntax
  2. Spring Boot Form

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
No ADS