首页 > 解决方案 > 如何将一个字段的值分配给 Thymeleaf 中其他对象的另一个字段?

问题描述

我想在单击Delete按钮后删除对象(表中的一行)(每行都有删除按钮。

如何th:object="${userToDelete}"在表中分配适当的行:逻辑此操作UserToDelete.id = user.id

 <form th:action="@{/admin/showUsers}" th:object="${userToDelete}" method="post">
  <table class="table table-striped">
    <thead class="thead-dark">
      <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Email</th>
        <th></th>
      </tr>
      </thead>
      </tbody>
      <tr th:each="user : ${users}">
       <td style="color: #000000" th:text="${user.id}">id</td>
       <td style="color: #000000" th:text="${user.name}">Name</td>
       <td style="color: #000000" th:text="${user.email}">Email</td>
       <td>
        <div class="form-group">
          <input type="submit" value="Delete"
            class="btn btn-lg btn-outline-primary btn-block">
        </div>
       </td>
      </tr>
    </tbody>
  </table>
</form>

标签: springthymeleaf

解决方案


td为每个添加tr

<td>
   <a th:href="${'/admin/delete/' + user.id}">delete</a>
</td>

然后添加GET端点进行删除 -/admin/delete/{id}


推荐阅读