英文:
@DeleteMapping spring boot
问题
DELETE查询为什么对我不起作用。
@DeleteMapping("/delete")
public String deleteUser(@RequestParam("userId") long id) {
userService.deleteUser(id);
return "redirect:/";
}
这个映射有一个表单,但是返回错误消息“Request method 'GET' is not supported”。
<form th:object="${users}">
<input type="button" value="Add" onclick="window.location.href = 'add'"/>
<input th:formaction="@{/update}" type="submit" value="Update" th:method="patch" class="button"/>
<input th:formaction="@{/delete}" type="submit" value="Delete" th:method="delete"/>
<select th:name="userId">
<option th:each="user : ${users}" th:value="${user.id}">
<tr>
<td th:text="${user.name}"></td>
<td th:text="${user.surname}"></td>
<td th:text="${user.age}"></td>
</tr>
</option>
</select>
</form>
如果我使用 @GetMapping,则一切正常,但我理解这是不正确的,因为在这种情况下不会执行删除请求。
如果我这样做,那么删除工作了,但PATCH不起作用。
<form th:object="${users}" th:method="delete">
<input type="button" value="Add" onclick="window.location.href = 'add'"/>
<input th:formaction="@{/update}" type="submit" value="Update" th:method="patch" class="button"/>
<input th:formaction="@{/delete}" type="submit" value="Delete"/>
<select th:name="userId">
<option th:each="user : ${users}" th:value="${user.id}">
<tr>
<td th:text="${user.name}"></td>
<td th:text="${user.surname}"></td>
<td th:text="${user.age}"></td>
</tr>
</option>
</select>
</form>
如何使userId在外部表单中可用,即在循环之外?
我已重新设计任务,这些表单可以单独工作。如何使这个选择器关联两个表单。
<select th:name="userId" id="userid" th:form="form_update | form_delete">
<option th:each="user : ${users}" th:value="${user.id}">
<tr>
<td th:text="${user.name}"></td>
<td th:text="${user.surname}"></td>
<td th:text="${user.age}"></td>
</tr>
</option>
</select>
<input type="button" value="Add" onclick="window.location.href = 'add'"/>
<form action="#" th:id="form_delete" th:action="@{/delete}" th:object="${users}" th:method="delete">
<input type="submit" value="Delete"/>
</form>
<form action="#" th:id="form_update" th:action="@{/update}" th:object="${users}" th:method="patch">
<input type="submit" value="Update"/>
</form>
英文:
why is the DELETE query not working for me.
@DeleteMapping("/delete")
public String deleteUser(@RequestParam("userId") long id) {
userService.deleteUser(id);
return "redirect:/";
}
There is a form for this mapping, but it returns me the Request method 'GET' is not supported.
<form th:object="${users}">
<input type="button" value="Add" onclick="window.location.href = 'add'"/>
<input th:formaction="@{/update}" type="submit" value="Update" th:method="patch" class="button"/>
<input th:formaction="@{/delete}" type="submit" value="Delete" th:method="delete"/>
<select th:name="userId">
<option th:each="user : ${users}" th:value="${user.id}">
<tr>
<td th:text="${user.name}"></td>
<td th:text="${user.surname}"></td>
<td th:text="${user.age}"></td>
</tr>
</option>
</select>
</form>
If I do @GetMapping, then everything works, but I understand that this is not correct, the delete request is not executed in this case.
If I do this, then delete works, but PATCH does not work.
<form th:object="${users}" th:method="delete">
<input type="button" value="Add" onclick="window.location.href = 'add'"/>
<input th:formaction="@{/update}" type="submit" value="Update" th:method="patch" class="button"/>
<input th:formaction="@{/delete}" type="submit" value="Delete"/>
<select th:name="userId">
<option th:each="user : ${users}" th:value="${user.id}">
<tr>
<td th:text="${user.name}"></td>
<td th:text="${user.surname}"></td>
<td th:text="${user.age}"></td>
</tr>
</option>
</select>
</form>
how to make userId available in an external form, that is, outside the loop
I have redone the task, these forms work separately. How do I make this select look at two forms.
<select th:name="userId" id="userid" th:form="form_update | form_delete" >
<option th:each="user : ${users}" th:value="${user.id}">
<tr>
<td th:text="${user.name}"></td>
<td th:text="${user.surname}"></td>
<td th:text="${user.age}"></td>
</tr>
</option>
</select>
<input type="button" value="Add" onclick="window.location.href = 'add'"/>
<form action="#" th:id="form_delete" th:action="@{/delete}" th:object="${users}" th:method="delete">
<input type="submit" value="Delete"/>
</form>
<form action="#" th:id="form_update" th:action="@{/update}" th:object="${users}" th:method="patch">
<input type="submit" value="Update"/>
</form>
答案1
得分: 2
HTML表单仅支持GET
和POST
方法。
如果您想使用DELETE
方法,您将需要使用JavaScript。
英文:
HTML forms support only GET
and POST
methods.
If you want to use a DELETE
method, you will have to use JavaScript.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论