【问题标题】:I cannot send PUT data with html form我无法使用 html 表单发送 PUT 数据
【发布时间】:2017-09-09 11:15:37
【问题描述】:

我有一个用 HTML 创建的表单

<form th:action="@{/forgotPassword}" th:object="${forgotPasswordDTO}" method="POST" role="form" id="forgotPasswordForm">
    <input type="hidden" name="_method" value="PUT"/>
...

我想向控制器发送数据

    @PutMapping("/forgotPassword")
    public ModelAndView recoverPassword(
            @ModelAttribute("forgotPasswordDTO") @Valid ForgotPasswordDTO forgotPasswordDTO,
            BindingResult result,
            ModelMap modelMap
    ) {
...

BindingResult 仍然向我返回错误,因为@ModelAttribute 对象的字段为空。

我可以使用 html 表单发送 PUT 查询吗?

【问题讨论】:

  • method="POST" ??
  • 什么'method="POST'?
  • 代码的第一行
  • HTML 只能采用 GET 和 POST 两种方法。但是,据说有一种方法可以在输入中声明另一种可以使用的方法。但问题是它不起作用。

标签: java html spring spring-boot put


【解决方案1】:

如果您使用的是 Spring Boot 1.5.6,那么我确实注意到您正在模拟的 Thymeleaf th:method="put" 似乎已损坏。替换为 th:method="post" 并将控制器方法更改为 @PostMethod 会导致表单开始正确填充和验证。恢复到版本 1.5.4 为我解决了这个问题。两个版本都应用了相同的过滤器,但 1.5.6 版本导致控制器中的表单为空。我检查了我的两个项目,它们表现出相同的行为。

【讨论】:

  • Spring boot 1.5.7 刚出来,似乎已经解决了问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-16
  • 2019-08-02
  • 2018-04-18
  • 2021-12-10
  • 1970-01-01
  • 1970-01-01
  • 2014-11-21
相关资源
最近更新 更多