【发布时间】:2021-01-07 10:15:40
【问题描述】:
我正在尝试向 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 boot 控制器方法:
@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;
}
我没有收到任何错误,所以我不确定出了什么问题。
【问题讨论】:
-
调用是否到达控制器方法?控制器类级别是否定义了任何 RequestMapping?
-
不,调用没有到达控制器方法。 RequestMapping 定义为 "@PostMapping("/change_password")"
-
我的意思是如果在类级别上总体定义了任何 RequestMapping。在这种情况下,它还必须附加在表单 URL 中。无论如何,我尝试了您的 html 和控制器代码并调用到达控制器。如果尝试了任何 URL,请确保检查浏览器的“网络”选项卡。
-
不,没有整体的 RequestMapping。我会尝试看看是否还有其他问题。谢谢!
标签: forms spring-boot controller thymeleaf form-submit