2016-07-23 753 views
2

我在简单的Spring Boot应用程序中有一个Thymeleaf模板。该模板在表格中包含如下列表:我可以在Spring Boot应用程序中从Thymeleaf表发出HTTP POST请求

<p>There are <span th:text="${#lists.size(persons)}"></span> people:</p> 
    <table th:if="${not #lists.isEmpty(persons)}" border="1"> 
     <tr>     
      <th>ID</th> 
      <th>Name</th>   
      <th>Address</th> 
      <th>Telephone</th> 
      <th>Email</th>  
      <th>Actions</th> 
     </tr> 
     <tr th:each="person : ${persons}">     
      <td th:text="${person.personId}"></td> 
      <td th:text="${person.name}"></td> 
      <td th:text="${person.address}"></td> 
      <td th:text="${person.telephone}"></td> 
      <td th:text="${person.email}"></td> 
      <td><a href="#" data-th-href="@{/edit(personId=${person.personId})}">Edit</a> | 
       <a href="#" data-th-href="@{/delete(personId=${person.personId})}">Delete</a></td> 
     </tr> 
    </table>  

我想根据表中最后一个单元格启用编辑和删除功能。但目前这两个请求都是针对HTTP GET的。对于从服务器获取某人的详细信息以进行编辑的编辑而言,这很好,但由于服务器上的数据更改,删除应该会触发POST请求。

有谁知道如果Thymeleaf允许每行表的POST请求?或者我必须每行写一个简单的HTML表单?

的GET形式是目前:

<td><a href="#" data-th-href="@{/edit(personId=${person.personId})}">Edit</a> 
        <!--a href="#" data-th-href="@{/delete(personId=${person.personId})}">Delete</a></td--> 

        <form method="get" th:action="@{/edit(personId=${person.personId})}">       
         <button type="submit" name="submit" value="value">Edit</button> 
        </form> 
       </td> 

在那里我有一个链接,用于测试的形式。

控制器方法被调用为:

// Gets a Person. 
    @RequestMapping(value="/edit", method=RequestMethod.GET) 
    public String getEditPerson(@RequestParam("personId") String personId, 
           Model model) {  
     logger.info(PersonController.class.getName() + ".getEditPerson() method called."); 

     Person person = personDAO.get(Integer.parseInt(personId)); 
     model.addAttribute("person", person); 

     // Set view.  
     return "/edit"; 

    }  

时,你得到的按钮版本被称为是错误:

Whitelabel Error Page 

This application has no explicit mapping for /error, so you are seeing this as a fallback. 

Sun Jul 24 00:26:16 BST 2016 
There was an unexpected error (type=Bad Request, status=400). 
Required String parameter 'personId' is not present` 

我使用GET来触发编辑,因为没有数据发送到personId以外的服务器。没有采取数据库操作,所以它应该是一个GET。

回答

3

你正在使用链接,我不认为这是可能的,你需要使用一个表单,您可以指定要使用的POST方法。

在下面IM在使用了<button>代替<a>元素,但它会工作,你需要做的唯一的事情是CSS样式您的按钮,看起来像你的链接

<form method="POST" th:action="@{/edit(personId=${person.personId})}"> 
     <button type="submit" name="submit" value="value" class="link-button">This is a link that sends a POST request</button> 
    </form> 

现在在你的代码看起来应该是这样

<tr th:each="person : ${persons}">     
      <td th:text="${person.personId}"></td> 
      <td th:text="${person.name}"></td> 
      <td th:text="${person.address}"></td> 
      <td th:text="${person.telephone}"></td> 
      <td th:text="${person.email}"></td> 
      <td> <form method="POST" th:action="@{/edit(personId=${person.personId})}"> 
     <button type="submit" name="submit" value="value" class="link-button">EDIT</button></form> | 
        <form method="POST" th:action="@{/delete(personId=${person.personId})}"> 
     <button type="submit" name="submit" value="value" class="link-button">DELETE</button></form> </td> 
     </tr> 

编辑

正如你刚刚分享你的Java代码,你AR控制器e期望该personId不作为PathVariable,但作为RequestParam, 在这种情况下,您的表单应具有该值...

编辑您的表单并添加人员ID如下。

<form method="POST" th:action="@{/edit}"> 
      <input type="hidden" name="personid" id="personId" th:value="${person.personId}" /> 
      <button type="submit" name="submit" value="value" class="link-button">This is a link that sends a POST request</button> 
     </form> 

还要注意,我改变了形式的行动,只是/编辑,为您的控制器是什么样子

+0

奇怪的是,在POST用于删除形式的作品,但得到一个用于编辑不。 –

+0

你能分享你得到的错误吗? –

+0

也为什么你不在编辑上使用POST? –

相关问题