![](/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.