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?
The types of expressions Thymeleaf supports are:
${...} - Variable Expressions. These are the standard expressions.*{...} - 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.#{...} - Message Expressions. These expressions are mainly used to externalize text. For example, to provide text in different languages by reading from a messages file.@{...} - Link URL Expressions. Used to generate URLs, see the standard url syntax.~{...} - Fragment Expression. Used to specify which fragment to include, see
fragment specification syntax.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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With