Thymeleaf template engine – content not showing due to my mistake

Thu, Apr 17, 2014

Java Programming #java #thymeleaf

Thymeleaf(http://www.thymeleaf.org/) is one of my favorite template engine when come to Spring MVC framework. It allows designer to draft out the HTML first and programmer can use attribute to populate the content accordingly. So today I was trying to embed some input field in a table column, and the input field just won’t show up. And here is my code.

<td th:text="${obj.badgeNo}">
  Badge No.
  <input type="hidden" th:value="*{badgeNo}"/>
</td>

I couldn’t figure out why, and Google doesn’t seem to help much as well. Few minutes later I realized I’ve made a mistake. The attribute “th:text” in the td tag will overwrite whatever text in the tag! Of course! How could I not see this. I really can’t blame anyone but myself for being so dumb. Sigh. I hope I won’t be making the same mistake again by writing this post and sharing it(although it’s kinda embarassing). So here are the correct code

<td>
  <span th:text="${obj.badgeNo}">Badge No.</span>
  <input type="hidden" th:value="*{badgeNo}"/>
</td>