Thymeleaf 表单未提交给 Spring 启动控制器

Thymeleaf form not submitting to Spring boot controller

我正在尝试向 Spring 引导控制器提交表单 这是百里香叶部分:

  <form th:action="@{/change_password}" method="post">
    <div class="row">
        <div class="col-md-9 register-right">
            <div class="tab-content" id="myTabContent">
                <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
                    <h3 class="register-heading">Change password</h3>
                    <div class="row register-form">
                        <div class="col-md-6">
                            <div class="form-group">
                                <input type="email" th:name="email" id="email" class="form-control" placeholder="Email *" >
                                <span th:if="${notPresent}" class="alert alert-info" style="color:red; width: 100% !important; border: none; background-color: transparent !important;">This email does not exist!</span>
                            </div>
                            <div class="form-group">
                                <input type="password" th:name="password" id="password" class="form-control" placeholder="Password *" >
                            </div>
                            <div class="form-group">
                                <input type="password" th:name="confirmPassword" id="confirmPassword" class="form-control"  placeholder="Confirm *" >
                            </div>
                        </div>
                        <div class="col-md-6">
                            <input type="submit" class="btnRegister" style="background-color: #ffa600 !important;" value="Change"/>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
  </form>

这是Spring引导控制器方法:

@PostMapping("/change_password")
    public String changeUserPassword(HttpServletRequest request, Model model) {
        String path = "";
        User u = userService.findByEmail(request.getParameter("email"));
        if(u == null || u.getActive() == 0) {
            model.addAttribute("notPresent", true);
            path =  "redirect:/forgot_password";
        } else {
            u.setPassword(bCryptPasswordEncoder.encode(request.getParameter("password")));
            userService.updateUser(u);
            sendEmail(u.getEmail(), u.getFirstname());
            path = "redirect:/login";
        }
        
        return path;
    }

我没有收到任何错误,所以我不确定哪里出了问题。

要提交表单,您可以使用在 Class 中定义的对象 userPassword,如下所示:

package ....
imports ....
@Data @AllArgsConstructor @NoArgsConstructor @ToString
public class UserPassword {

  private Integer id;
  private String password;
  ... other fields you need
}

您将此对象添加到显示页面的控制器:

 model.addAttribute("UserPassword", userPassword);

然后将此对象添加到表单中:

<form th:action="@{/change_password}" th:object="${userPassword}" method="post">

您需要在您的 html 页面中将数据绑定到该对象(这里我将 passworduserPassword 绑定到输入标签):

 <input type="password" th:field="*{password}"  class="form-control" placeholder="Password *" >

最后我在控制器中检索对象:

@PostMapping("/change_password")
public String changeUserPassword(Model model, @ModelAttribute UserPassword userPassword) {
.....
}

您可以在 https://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html

中找到第 7 部分的完整教程