Search code examples
javahtmlthymeleaf

Thymeleaf th:field overwrites th:value


I just figured out that the th:field attribute overrides the th:value attribute.

In my th:each I want to override the value that th:field generates for me. Does anyone know how to achieve this?

Thanks in advance.

<ul id="userGroupContainer">
	<li class="clickable unselected" th:each="u, rowStat : ${userNotInGroup}" th:if="${u.id}">
      <input type="text" readonly="readonly" hidden="hidden" disabled="disabled" th:field="* {users[__${rowStat.index}__].id}" th:value="${u.id}" />
      <input type="text" readonly="readonly" hidden="hidden" disabled="disabled" th:field="* {users[__${rowStat.index}__].displayName}" th:value="${u.displayName}" />
      <input type="text" readonly="readonly" hidden="hidden" disabled="disabled" th:field="* {users[__${rowStat.index}__].username}" th:value="${u.username}" />
      <input type="text" readonly="readonly" hidden="hidden" disabled="disabled" th:field="*{users[__${rowStat.index}__].emailAddress}" th:value="${u.emailAddress}" />
								<span th:text="${u.displayName}"></span>
	</li>
</ul>


Solution

  • Thymeleaf th:field generates 3 html attributes id, name an value.

    For your case, instead of using th:field, use th:id, th:name and th:value as shown below.

    <input type="text" th:id="${'users'+__${rowStat.index}__+'.id'}" th:name="${'users['+__${rowStat.index}__+'].id'}" th:value="${u.id}" readonly="readonly" hidden="hidden" disabled="disabled"/>
    <input type="text" th:id="${'users'+__${rowStat.index}__+'.displayName'}" th:name="${'users['+__${rowStat.index}__+'].displayName'}" th:value="${u.displayName}" readonly="readonly" hidden="hidden" disabled="disabled"/>