Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 600
pmaurais Avatar asked Dec 11 '22 02:12

pmaurais


2 Answers

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.
like image 169
Metroids Avatar answered Mar 04 '23 23:03

Metroids


The documentation provides very nice examples so give it a look.

In short:

${} used for variable expressions. Variable expressions are OGNL expressions –or Spring EL if you’re integrating Thymeleaf with Spring

*{} is used for selection expressions. Selection expressions are just like variable expressions, except they will be executed on a previously selected object.

#{} is used for message (i18n) expressions. Used to retrieve locale-specific messages from external sources

like image 30
stackFan Avatar answered Mar 04 '23 21:03

stackFan