Difference between th:text and th:value in Thymeleaf

Viewed 20453

I just recently started using Thymeleaf through one of my projects. I have seen few examples where th:text=${example} is being used in some places th:value=${example}.

I have gone through the Thymeleaf documentation but couldn't find anything explicitly citing the difference, nor did any question on SO.

Any help would be really appreciated! Thanks.

3 Answers
  • th:value is modification of html attribute value. For button, input and option elements, the value attribute specifies the initial value of the element
  • th:text is used for tag body modification.

div{background-color: lightblue; padding: 2px} // to highlight empty div
<!--th code:               <div th:value="${value}"/></div> -->
<br/>Result th:value div:  <div value="sometext"/></div>
        
<!--th code:               <form><input th:value="${value}"/></form>-->
<br/>Result th:value form: <form><input value="sometext"></form> 

<!--th code:               <div th:text="${value}"></div> 
Same as:                   <div>[[${value}]]</div> -->
<br/>Result th:text div:   <div>sometext</div>

Here is docs of different Thymeleaf attributes features

th:name => This would be the name of the value that you will be either passing to another page (Exemplar scenario).

th:value => This would be the actual value that you would be passing. It could be obtained from a model or straight from the database explicitly.

<form th:action="@{confirm-pass-details.html}">
    <button type="submit" th:name="event-id" th:value="${event.get().getEventid()}">Buy Passes</button>
</form>

Just an example

Related