我使用从"start.Spring.io"安装的Spring Boot v2.7.0,并从那里安装了Thymeleaf,当我在父pom中搜索时,我发现:
最近,我需要应用模式<form th:method="put/delete".../>.
Step 1:
在application.properties中添加此属性:
spring.mvc.hiddenmethod.filter.enabled=true
我在application.yaml中进行了try (作为第二种解决方案,因为前一种方案不起作用),如下所示:
spring:
mvc:
hiddenmethod:
filter:
enabled: true
Step 2:
我使用了:
<form th:method="put".../>
<form th:method="delete".../>
Step 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)
在google搜索之后,我找到了这个解决方案,通过以下方式自己添加所需的bean,其中DID WORKED:
@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,而我必须自己添加它.
有人能帮我吗?