Friday, February 9, 2018

Thymeleaf hidden field value

Greetings!

Thymeleaf gives us almost everything we need. But there are some cases that thymeleaf does not work properly.
When we have a hidden field and we want to assign it a value, strangely thymeleaf will ignore the value property.

Solution:
Use th:attr
th:attr="name='

<tr th:each="user, stat : ${users}">    <input type="hidden"           th:attr="name='userDtos[' + ${stat.index} + '].userId'"           th:value="${user.id}" />    <!--some other inputs-->    <td>        <input type="checkbox" th:field="*{userDtos[__${stat.index}__].active}" />    </td></tr>

3 comments:

  1. I just had this problem, this worked for me. Thank you Manju!
    I can't believe this works and th:field doesn't!

    ReplyDelete
  2. Two days searching for a solution.
    Thank you Manju! Thank you really much.

    ReplyDelete