Search code examples
spring-bootspring-mvcthymeleaf

Difference between *, $, and #


I am new to thymeleaf and see these three operators often. What is the difference between *{} ${} and #{}?

I know they are for accessing data from the MVC but in what context?


Solution

  • The types of expressions Thymeleaf supports are:

    1. ${...} - Variable Expressions. These are the standard expressions.
    2. *{...} - Selection Variable Expressions. These are the same as variable expressions, except that they are used in combination with a a th:object attribute. For example, if you have <form th:object="${form}">, then the expression *{field} resolves to ${form.field}. These are mostly used when using th:field attributes while creating a form.
    3. #{...} - Message Expressions. These expressions are mainly used to externalize text. For example, to provide text in different languages by reading from a messages file.
    4. @{...} - Link URL Expressions. Used to generate URLs, see the standard url syntax.
    5. ~{...} - Fragment Expression. Used to specify which fragment to include, see fragment specification syntax.