What's the difference between data-th-text and th:text in Spring thymeleaf

Viewed 1415

I am new in spring thymeleaf and I am not able to get difference between these two data-th-text and th:text.

Can anyone explain the difference, with the help of examples, and when should I use data-th-text and th:text ?

1 Answers

Both does the same thing but as per thymeleaf docs:

Non-standard attributes we are using in the th:* form are not allowed by the HTML5 specification. To make your templates HTML5-valid use data- prefix for attribute names and hyphen (-) separators instead of semi-colons (:)

Ref: https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#using-texts

This is not HTML5 valid:

 <p th:text="#{home.welcome}">Welcome to our grocery store!</p>

This is HTML5 valid:

<p data-th-text="#{home.welcome}">Welcome to our grocery store!</p>
Related