在没有模型的情况下使用 th:object

时间:2021-07-27 07:07:21

标签: java spring thymeleaf

我想用 th:object 为数组的每个元素创建表单。但是我出错了。问题是 th:object 仅用于保存在模型上的对象。如何在 th:object 上使用 th:fieldth:each 或任何其他想法来解决它?

<tr th:each="sect: ${process.sectionData}" th:if="${sect.sectionType==1}">
       <form th:action="@{'/process/section/save/' + ${sect.id} }" method="post" th:object="${sect}">
          <td>
             <button>
                save
             </button>
          </td>
          <td>
             <input
                 type="text"
                 th:field="*{sect.name}"
                 class="m-inline"
                 placeholder="Страна проведения"/>
          </td>
        </form>
</tr> 

1 个答案:

答案 0 :(得分:0)

th:field 只是设置输入字段的 nameidvalue 的快捷方式。您应该能够做到这一点并让它发挥作用:

<tr th:each="sect: ${process.sectionData}" th:if="${sect.sectionType==1}">
  <form th:action="@{'/process/section/save/' + ${sect.id}}" method="post">
    <td>
       <button>save</button>
    </td>
    
    <td>
       <input
          type="text"
          name="name"
          th:value="${sect.name}"
          class="m-inline"
          placeholder="Страна проведения" />
    </td>
  </form>
</tr> 
相关问题