在百里香中删除或放置方法

use*_*455 10 spring-mvc thymeleaf

我想@RequestMapping(value = "", method = RequestMethod.DELETE)在春天从百里香的形式打电话.

是否有可能从百里香叶调用删除或放置请求映射方法?

请对此提出建议.

mar*_*cok 20

你可以使用th:method这个:

<form th:object="${commandObject}" th:action="@{/urlToCall}" th:method="delete">
Run Code Online (Sandbox Code Playgroud)

这将生成一个隐藏的输入元素,将由Spring MVC拾取:

<input type="hidden" name="_method" value="delete">
Run Code Online (Sandbox Code Playgroud)

  • 对我来说,我必须将 spring.mvc.hiddenmethod.filter.enabled=true 添加到我的 application.properties 文件中才能工作。 (3认同)

ene*_*ral 10

当我们使用 时th:method="PUT",thymeleaf 会创建隐藏的输入,如下面的屏幕截图所示。然后HiddenHttpMethodFilter将发布的方法参数转换为 HTTP 方法。

httpmethodfilter-转换

在 Spring Boot 2.2 中默认禁用HiddenHttpMethodFilter 。

您可以通过添加spring.mvc.hiddenmethod.filter.enabled=true到 application.properties 文件来启用此功能。


Sot*_*lis 6

Thymeleaf是一个HTML模板引擎.HTML不支持其属性的HTTP方法putdeleteHTTP方法method.所以,不,你不能.但是,你有其他选择.

您可以使用javascript异步发送请求.在这种情况下,您可以发送任何类型的HTTP请求.

您还可以使用HiddenHttpMethodFilter带有此处所述hidden _method=put <input>元素的a .就像是

<input name="_method" type="hidden" value="PUT" />
Run Code Online (Sandbox Code Playgroud)