我不能用html格式发送PUT数据

时间:2021-10-13 19:37:19

I have a form created in HTML

我有一个用HTML创建的表单

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

and I want to send data to the controller

我想将数据发送到控制器

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

but BindingResult still returns errors to me because the fields of the @ModelAttribute object are null.

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

Can I send a PUT query using html form?

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

1 个解决方案

#1


0  

If you are using Spring Boot 1.5.6 then I did notice the Thymeleaf th:method="put", which you are emulating, seems to be broken. Replacing with th:method="post" and changing the controller method to a @PostMethod results in the form begin populated and validated correctly. Reverting back to version 1.5.4 corrected the problem for me. The same filters are being applied in both versions but the 1.5.6 version results in an empty form in the controller. I have checked two of my projects and they exhibit the same behavior.

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

#1


0  

If you are using Spring Boot 1.5.6 then I did notice the Thymeleaf th:method="put", which you are emulating, seems to be broken. Replacing with th:method="post" and changing the controller method to a @PostMethod results in the form begin populated and validated correctly. Reverting back to version 1.5.4 corrected the problem for me. The same filters are being applied in both versions but the 1.5.6 version results in an empty form in the controller. I have checked two of my projects and they exhibit the same behavior.

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