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

发布于 2025-02-10 16:11:25 字数 2025 浏览 1 评论 0原文

我使用从“ start.spring.io”安装的Spring Boot v2.7.0,然后从那里安装了胸腺,当我在父pom中搜索时,我发现: 胸腺pring5(v3.0.15.Release),胸腺extras-java8time(v3.0.4.4.Release)

,我需要应用模式< form th th:method th:method th:put/delete'.../delete“ ...//delete” .../ > 。 在Verious的地方进行了谷歌搜索后,我找到了该解决方案,该解决方案也在书中也被重新提到: “ 驯服胸腺的实用指南,构建具有弹簧靴和百里角的网络应用程序-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:

最后我使用了:“ @put> @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 。

有人可以帮助我吗? 非常感谢

I use Spring Boot v2.7.0, installed from "start.spring.io" and from there I installed Thymeleaf, and as I searched in the parent-pom I found out that:
thymeleaf-spring5 (v3.0.15.RELEASE), thymeleaf-extras-java8time (v3.0.4.RELEASE)

Lately, I needed to apply the pattern <form th:method="put/delete".../>.
After googling in verious places, I found the solution, which was reffered in the book as well:
"Taming Thymeleaf Practical Guide to building a web application with Spring Boot and Thymeleaf - Wim Deblauwe"
which is the top/excellent books of Thymeleaf, and from which I learn Thymeleaf.
Acoording to these, I did:

Step 1:

Added this property in application.properties:

spring.mvc.hiddenmethod.filter.enabled=true

and I tried it in the application.yaml (as a 2nd solution, because the previous did not work), like this way:

spring:
  mvc:
    hiddenmethod:
      filter:
        enabled: true

Step 2:

I used:

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

Step 3:

Finally I used the: "@PutMapping, @DeleteMapping" in my controller handler methods.

The result was the error message:

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)

After googling I found this solution, to add the needed bean by myself with the following way, which DID WORKED:

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

I wonder why this configuration "spring.mvc.hiddenmethod.filter.enabled=true", does not add the needed bean in my case, and I have to add it by myself.

Anyone can help me on this, please?
Thanks a lot in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿哥们儿 2025-02-17 16:11:26

我自己只是进行了测试,这很好。

  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. 创建index.html preation src中的 file /main/resources/templates :
<!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. update application.properties要包含:
spring.mvc.hiddenmethod.filter.enabled=true
  1. 启动应用程序并访问http:// localhost:8080:8080并在输入字段中输入某些内容。按下保存时,将其打印到控制台上,显示@putMapping工作。

I just did a test myself and this worked fine.

  1. Create a new project on start.spring.io selecting Spring Boot 2.7.1 with dependencies Spring Web and Thymeleaf
  2. Create a controller:
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:/";
    }
}

With this form data class:

public class TestFormData {
    private String someString;

    public String getSomeString() {
        return someString;
    }

    public void setSomeString(String someString) {
        this.someString = someString;
    }
}
  1. Create an index.html file in src/main/resources/templates:
<!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. Update application.properties to contain:
spring.mvc.hiddenmethod.filter.enabled=true
  1. Start the application and go to http://localhost:8080 and enter something in the input field. When pressing save, it is printed to the console, showing that the @PutMapping works.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文