【发布时间】:2016-11-27 12:25:18
【问题描述】:
我在一个简单的 Spring Boot 应用程序中有一个 Thymeleaf 模板。该模板包含一个表中的列表,如下所示:
<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";
}
按钮版GET调用时的错误是:
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。
【问题讨论】:
标签: html http-post thymeleaf http-get