有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Thymeleaf,无法访问该参数

我试图在thymeleaf中获得值以传递给delete方法,但我不能。请帮忙

我有一张这样的桌子,它在工作:

<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>

但我不知道如何通过这个客户。ipsid到以下删除方法链接中的id:

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

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

</form>

共 (1) 个答案

  1. # 1 楼答案

    你需要通过临时客户。在表单中按submit时的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";
    }