Search code examples
javahtmlspring-bootthymeleaf

Using <input type="hidden" /> combine with thymeleaf


I have a code to iterate a list sent from Controller to View page like this:

<tbody>
    <th:block th:each="job,iterStat : ${listJob}">
        <tr class="job-detail" th:if="${job.status == 'success'}">
            <td th:text='${iterStat.index}+1'><input name="id"
                                    type="hidden" value="th:text='${job.id}'" /></td>
            <td th:text='${job.name}'></td>
            <td th:text='${job.time}'></td>
            <td>yyyy/mm/dd hh24/mi/ss</td>
            <td>yyyy/mm/dd hh24/mi/ss</td>
        </tr>
        <tr class="danger job-detail" th:if="${job.status == 'danger'}">
            <td th:text='${iterStat.index}+1'><input name="id"
                                    type="hidden" value="th:text='${job.id}'" /></td>
            <td th:text='${job.name}'></td>
            <td th:text='${job.time}'></td>
            <td>yyyy/mm/dd hh24/mi/ss</td>
            <td>yyyy/mm/dd hh24/mi/ss</td>
        </tr>
    </th:block>
</tbody>

I have a hidden value in first td:

<td th:text='${iterStat.index}+1'><input name="id"
                                type="hidden" value="th:text='${job.id}'" /></td>

But when I check it the hidden value was undefined. Here is the image show that input element was not generated. I dont know what to do this issue :( Havent met it before. enter image description here


Solution

  • I believe th:text='${iterStat.index}+1' overwritten the hidden input. You also might want th:value in the input.

    If you want both, try this.

    <td>
        <th:block th:text='${iterStat.index}+1'></th:block>
        <input name="id" type="hidden" th:value="${job.id}" />
    </td>