Thymeleaf,无法访问参数

时间:2019-04-25 06:23:31

标签: java spring thymeleaf

我尝试在百里香中获取价值以传递给删除方法,但我做不到。请帮忙。

我有这样的表,它正在工作:

<tbody>

<tr th:each="tempCustomer : ${customer}">

<td th:text="${tempCustomer.ipsid}" />

<td th:text="${tempCustomer.docnumber}" />

<td th:text="${tempCustomer.fullname}" />

<td th:text="${tempCustomer.nickname}" />

<td th:text="${tempCustomer.gender}" />

<td th:text="${tempCustomer.placeofbirth}" />

<td th:text="${tempCustomer.fincode}" />

<td th:text="${tempCustomer.status}" />

</tr>

</tbody>

但是我不知道如何将tempCustomer.ipsid传递给以下删除方法链接中的ID:

<form action="#" th:action="@{delete/id}">

<button type="submit" class="btn btn-primary btn-sm mb-3">Delete</button>

</form>

1 个答案:

答案 0 :(得分:0)

当您按表单中的提交时,您需要传递tempCustomer.id。为此,您需要以这种形式添加输入:

<tr th:each="tempCustomer : ${customer}">

<td th:text="${tempCustomer.ipsid}" />

<td th:text="${tempCustomer.docnumber}" />

<td th:text="${tempCustomer.fullname}" />

<td th:text="${tempCustomer.nickname}" />

<td th:text="${tempCustomer.gender}" />

<td th:text="${tempCustomer.placeofbirth}" />

<td th:text="${tempCustomer.fincode}" />

<td th:text="${tempCustomer.status}" />
<form th:action="@{delete}" method="post">
<input type="hidden" name="id" th:value="${tempCustomer.id}" />
<input type="submit" value="Delete" class="btn btn-danger" />
</form>
</td>
</tr>

然后在控制器中它将如下所示:

   @RequestMapping(value = "/delete", method = RequestMethod.POST)
private String deleteTempCustomer(@RequestParam String id){
    System.out.println("TempCustomer : "+id);
    service.deleteCustomer(id)//here if You need Long use Long.valueOf(id);
    return "redirect:/display";
}