Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring Thymeleaf-按钮单击调用http删除方法_Spring_Spring Mvc_Thymeleaf - Fatal编程技术网

Spring Thymeleaf-按钮单击调用http删除方法

Spring Thymeleaf-按钮单击调用http删除方法,spring,spring-mvc,thymeleaf,Spring,Spring Mvc,Thymeleaf,我想通过http delete方法调用url。我尝试了th:onclick和th:action,但没有成功 html代码: <button id="delete" name="delete" th:onclick="@{'/foos/{id}'(id=${foo.id})}" th:method="delete">Delete</button> 我想您的交易需要一份表格。还有这个隐藏的输入字段 <form action="#" th:action="@{'/del

我想通过http delete方法调用url。我尝试了th:onclick和th:action,但没有成功

html代码:

<button id="delete" name="delete" th:onclick="@{'/foos/{id}'(id=${foo.id})}" th:method="delete">Delete</button>

我想您的交易需要一份
表格
。还有这个隐藏的输入字段

<form action="#" th:action="@{'/delete/{id}'(id=${foo.id})}" th:method="delete" >
  <input type="hidden" name="_method" value="delete" />
  <button type="submit" id="submitButton"> </button>
</form>

th:method=“delete”会自动为您创建隐藏的输入字段。如果您手动添加它,您将拥有它两次。检查源代码

在这里的推荐之后,我仍然收到POST错误消息。我发现Spring默认情况下会忽略这些隐藏字段。解决方案是在
应用程序中激活它。属性
文件:

spring.mvc.hiddenmethod.filter.enabled=true
我的应用程序中的工作代码如下所示:

表格:


这是一个锅炉板?不是吗?@richersoon不,不是。我试过了,它对我有效。我喜欢窗体方式,而不是使用按钮。也许它改变了,但我最后的信息是,thymeleaf不提供put和delete方法。因此,您需要帮助thymeleaf了解您想要做什么。因此输入字段。当我尝试此操作时,会发送一个post请求,尽管输入的值是delete,方法是delete。。。。
spring.mvc.hiddenmethod.filter.enabled=true
<form action="#" th:action="@{'/books/delete/{id}'(id=${book.id})}" th:method="delete" >
    <button type="submit" class="btn">
        Delete
    </button>
</form>
@RequestMapping(value="/books/delete/{id}", method = RequestMethod.DELETE)
public String deleteBook(@PathVariable Long id) {
    bookService.deleteBook(id);
    return "books";
}