Thymeleaf表单未提交给Spring Boot Controller [英] Thymeleaf form not submitting to Spring boot controller
问题描述
我正在尝试向Spring引导控制器提交表单这是百里香叶部分:
I am trying to submit a form to Spring boot controller This is the thymeleaf part:
<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引导控制器方法:
This is the Spring boot controller method:
@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;
}
我没有收到任何错误,所以我不确定是怎么回事.
I don't get any errors so I am not sure what's wrong.
推荐答案
要提交表单,您可以在此类中使用对象userPassword定义,如下所示:
To submit a form you can use an object userPassword define in a Class like this:
package ....
imports ....
@Data @AllArgsConstructor @NoArgsConstructor @ToString
public class UserPassword {
private Integer id;
private String password;
... other fields you need
}
使用以下命令将该对象添加到显示页面的控制器中:
You add this object to the controller displaying the page with :
model.addAttribute("UserPassword", userPassword);
然后将这个对象添加到表单中:
Then you add this object to your form:
<form th:action="@{/change_password}" th:object="${userPassword}" method="post">
您需要将数据绑定到html页面中的此对象(此处,我将 password 从 userPassword 绑定到输入标签):
You need to bind data to this object in your html page (here I bind password from userPassword to the input tag):
<input type="password" th:field="*{password}" class="form-control" placeholder="Password *" >
最后我在控制器中检索对象:
And Finnaly I retrieve the object in the controller:
@PostMapping("/change_password")
public String changeUserPassword(Model model, @ModelAttribute UserPassword userPassword) {
.....
}
您可以在 https://www中找到第7部分的完整教程.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html
这篇关于Thymeleaf表单未提交给Spring Boot Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!