我使用从"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,而我必须自己添加它.

有人能帮我吗?

推荐答案

我自己做了个测试,效果很好.

  1. 开始时创建新项目.春天io Select 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正常工作.

Java相关问答推荐

Springdoc Whitelabel Error Page with Spring V3

使用java访问具体子类特定方法的最佳方法是什么?

如何打印本系列的第n项y=-(1)-(1+2)+(1+2+3)+(1+2+3+4)-(1+2+3+4+5)...Java中的(1+2+3+4...+n)

使用Testcontainers与OpenLiberty Server进行集成测试会抛出SocketException

具有多种令牌类型和段的复杂Java 17正则表达式

相同的Java SerializedLambda为implMethodKind返回不同的结果

Java中的死锁及其重入锁和锁

在Java中将int[]矩阵添加到ArrayList中,但出现错误

Kotlin Val是否提供了与Java最终版相同的可见性保证?

扩展视图高度,并将其拖动到较低的视图上,而不是将其向下推?

如何在太阳系模拟器中添加月球?

当我在Java中有一个Synchronized块来递增int时,必须声明一个变量Volatile吗?

Java 21中泛型的不兼容更改

在Spring Boot JPA for MySQL中为我的所有类创建Bean时出错?

从12小时开始的日期模式

在应用getCellFormula()时,Excel引用中的文件名始终为";[1]";使用Apache POI()

如何在Java中使用正则表达式拆分字符串

模拟JUnit未检测到返回字符串的方法的任何声纳覆盖

Java 21内置http客户端固定运营商线程

将在 Docker 中运行的 Spring Boot 连接到在 Docker 中运行的 PostgreSQL,无需 compose 文件?