百里香叶 - th:method= "delete/put"导致:不支持请求方法 'POST'



我使用的是Spring Boot v2.7.0,从"start.spring.io"然后我安装了Thymeleaf,当我在parent-pom中搜索时,我发现:thymleaf -spring5 (v3.0.15.RELEASE), thymleaf -extras-java8time (v3.0.4.RELEASE)

最近,我需要从:method="put/delete"…/>应用模式<.在各种地方搜索后,我找到了解决方案,这也是书中提到的:>使用Spring Boot和thymleaf构建web应用程序的教程- Wim Deblauwe";这是最优秀的书籍,我从中学习了百里叶。根据这些,我做了:

步骤1:

application.properties中添加此属性:

spring.mvc.hiddenmethod.filter.enabled=true

,我在application.yaml中尝试过(作为第二个解决方案,因为前一个不起作用),像这样:

spring:
mvc:
hiddenmethod:
filter:
enabled: true

步骤2:

我使用:

<form th:method="put".../>
<form th:method="delete".../>

步骤3:

最后我使用了:">@PutMapping,@DeleteMapping";在我的控制器处理程序方法。

结果是错误消息:

There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'POST' not supported
org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported
at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:253)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:442)

经过谷歌我找到了这个解决方案,我自己用以下方法添加所需的bean,其中确实工作:

@Bean
public FilterRegistrationBean<HiddenHttpMethodFilter> hiddenHttpMethodFilter() {
FilterRegistrationBean<HiddenHttpMethodFilter> filterRegistrationBean = new FilterRegistrationBean<>(new HiddenHttpMethodFilter());
filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
return filterRegistrationBean;
}

我想知道为什么这个配置"spring.mvc.hiddenmethod.filter.enabled=true"在我的情况下没有添加所需的bean,我必须自己添加它。

有谁能帮我一下吗?提前谢谢你

我刚刚自己做了一个测试,这个工作得很好。

  1. 在start.spring.io上创建一个新项目,选择Spring Boot 2.7.1,依赖于Spring Web和Thymeleaf
  2. 创建控制器:
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping
@Controller
public class TestController {
@GetMapping
public String index(Model model) {
model.addAttribute("formData", new TestFormData());
return "index";
}
@PutMapping
public String doPut(@ModelAttribute("formData") TestFormData formData) {
System.out.println("formData.getSomeString() = " + formData.getSomeString());
return "redirect:/";
}
}

使用这个表单数据类:

public class TestFormData {
private String someString;
public String getSomeString() {
return someString;
}
public void setSomeString(String someString) {
this.someString = someString;
}
}
  1. src/main/resources/templates中创建index.html文件:
<!DOCTYPE html>
<html lang="en"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:action="@{/}" th:method="put" th:object="${formData}">
<input th:field="*{someString}">
<button>Save</button>
</form>
</body>
</html>
  1. 更新application.properties包含:
spring.mvc.hiddenmethod.filter.enabled=true
  1. 启动应用程序,转到http://localhost:8080并在输入字段中输入一些内容。按下save,打印到控制台中,表示@PutMapping工作。