![](/img/trans.png)
[英]Spring MVC Thymeleaf Error when attempting to hook a form into controller
[英]Spring MVC4 + Thymeleaf Form data to Controller
我正在使用Thymeleaf作為模板引擎的Spring MVC4應用程序上工作。 我有一個包含團隊成員信息的表,該表在每行中都有一個嵌入式表單,並帶有刪除按鈕,以允許逐行刪除用戶。 以下是表格部分和當前屏幕截圖...
<table class="bordered">
<thead>
<tr>
<th data-field="id">Name</th>
<th data-field="name">Password</th>
<th data-field="price">Email</th>
</tr>
</thead>
<tbody>
<tr th:each="user : ${users}">
<td th:text="${user.userName}">Name</td>
<td th:text="${user.password}">Password</td>
<td th:text="${user.email}">Email</td>
<td>
<!-- Submit the user to be deleted -->
<form th:object="${user}" class="col s12" action="/deleteUser" method="post">
<button class="btn waves-effect waves-light red col" type="submit" name="deleteUser">
Delete
</button>
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
</form>
</td>
</tr>
</tbody>
</table>
當我單擊刪除按鈕時, TeamController接收到請求,但所有實例變量均為null,這對Spring MVC4和Thymeleaf還是很陌生,所以不確定這是否有效嗎? 我假設表單中的對象是對行中用戶對象的引用,所以不確定為什么它在控制器端為null?
注意:我以表單形式傳遞的用戶對象是位於模型中的UserAccount.java的實例。
能夠通過以下對我表格的更新對其進行修復...
<td>
<!-- Submit the user to be deleted -->
<form th:object="${UserAccount}" class="col s12" th:action="@{/deleteUser}" method="post">
<button class="btn waves-effect waves-light red col" type="submit" name="deleteUser">Delete</button>
<input type="hidden" id="userName" name="userName" th:value="${user.userName}" />
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
</form>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.