1

I am new to thymeleaf and I don't understand the difference between th:field="${something}" and th:field="*{something}". When to use * and when to use $?

soorapadman
  • 4,451
  • 7
  • 35
  • 47
Varun
  • 67
  • 1
  • 11
  • Did you check this ?https://stackoverflow.com/questions/37728515/thymeleaf-the-difference-between-thfield-and-thfield/40028263 – soorapadman Nov 21 '19 at 14:25
  • Does this answer your question? [Thymeleaf - The difference between th:field="${}" and th:field="\*{}"](https://stackoverflow.com/questions/37728515/thymeleaf-the-difference-between-thfield-and-thfield) – Jorge.V Nov 21 '19 at 15:10
  • @Varun if you found my answer helpful and solving your question you can mark it as an Accepted answer. – Nergon Nov 22 '19 at 13:04

1 Answers1

2

Quoting the thymeleaf documentation

Variable expressions not only can be written in ${...} expressions, but also in *{...} ones.

There is an important difference, though: the asterisk syntax evaluates expressions on selected objects rather than on the whole context variables map. This is: as long as there is no selected object, the dollar and the asterisk syntaxes do exactly the same.

From here in the 4.3 Expressions on selections (asterisk syntax) section.

Technically the difference is that if you have selected an Object like so th:object="${session.user} then you can use the * for the properties of the user object like so th:text="*{lastName}"

If you haven't selected an object they are the same.

Nergon
  • 443
  • 2
  • 14