[英]registration form is not submitting in spring boot and not hitting endpoints
I'm working on a registration form in Spring Boot
and thymeleaf but it's not submitting to the database.我正在使用
Spring Boot
和 thymeleaf 中的注册表单,但它没有提交到数据库。 I even run the debugger mood and it did not hit the endpoints for the registration.我什至运行调试器心情,它没有达到注册的端点。 Note, that my login and other functionalities work fine.
请注意,我的登录和其他功能工作正常。 I'm following this here to create my registration
我在这里关注这个来创建我的注册
How to I get this endpoint to create a new user?如何让这个端点创建一个新用户?
register.html
<form th:action="@{/registration}" th:object="${user}" method="post">
<p class="error-message" th:if="${#fields.hasGlobalErrors()}"
th:each="error : ${#fields.errors('global')}" th:text="${error}">Validation
error</p>
<div class="form-group"
th:classappend="${#fields.hasErrors('firstName')}? 'has-error':''">
<label for="firstName" class="control-label">First name</label> <input
id="firstName" class="form-control" th:field="*{firstName}" />
<p class="error-message"
th:each="error: ${#fields.errors('firstName')}"
th:text="${error}">Validation error</p>
</div>
<div class="form-group"
th:classappend="${#fields.hasErrors('lastName')}? 'has-error':''">
<label for="lastName" class="control-label">Last name</label> <input
id="lastName" class="form-control" th:field="*{lastName}" />
<p class="error-message"
th:each="error : ${#fields.errors('lastName')}"
th:text="${error}">Validation error</p>
</div>
<div class="form-group"
th:classappend="${#fields.hasErrors('email')}? 'has-error':''">
<label for="email" class="control-label">E-mail</label> <input
id="email" class="form-control" th:field="*{email}" />
<p class="error-message"
th:each="error : ${#fields.errors('email')}" th:text="${error}">Validation
error</p>
</div>
<div class="form-group"
th:classappend="${#fields.hasErrors('username')}? 'has-error':''">
<label for="email" class="control-label">Username</label> <input
id="email" class="form-control" th:field="*{username}" />
<p class="error-message"
th:each="error : ${#fields.errors('username')}" th:text="${error}">Validation
error</p>
</div>
<div class="form-group"
th:classappend="${#fields.hasErrors('password')}? 'has-error':''">
<label for="password" class="control-label">Password</label> <input
id="password" class="form-control" type="password"
th:field="*{password}" />
<p class="error-message"
th:each="error : ${#fields.errors('password')}"
th:text="${error}">Validation error</p>
</div>
<!--
<div class="form-group">
<button type="submit" class="btn btn-success">Register</button>
<span>Already registered? <a href="/" th:href="@{/login}">Login
here</a></span>
</div>
-->
<div class="form-group">
<button type="submit" class="btn btn-success">Register</button>
</div>
</form>
controller.java
@Controller
public class RegisterationController {
@Autowired
private UserService userService;
@GetMapping("/register")
public String getRegisterPage(ModelMap model) {
User user = new User();
model.put("user", user);
return "register";
}
@PostMapping("/registration")
public String postRegister(@RequestBody User user, BindingResult result) {
User existingUser = userService.findByEmail(user.getEmail());
if(existingUser != null) {
result.rejectValue("email", null, "this email is already used");
}
if (result.hasErrors()) {
return "register";
}
userService.saveNewUser(existingUser);
return "redirect:/";
}
}
UserService.java
public void saveNewUser(User user) {
User nuser = new User();
nuser.setUsername(user.getUsername());
nuser.setEmail(user.getEmail());
nuser.setPassword(user.getPassword());
nuser.setFirstName(user.getFirstName());
nuser.setLastName(user.getLastName());
// TODO: is this how you set the role of the user?
nuser.setRoles((Set<Role>) Arrays.asList(new Role("user")));
userRepository.save(nuser);
}
I solved this problem by changing two things;我通过改变两件事解决了这个问题; first, I created a custom
saveNewUser()
but JPA manages that better so I changed that to this.首先,我创建了一个自定义
saveNewUser()
但 JPA 管理得更好,所以我把它改成了这个。
UserService.java
public User save(User user) {
user.setRoles(new HashSet<Role> (Arrays.asList(new Role("user"))));
return userRepository.save(user);
}
I also had to change my endpoint to this我还必须将我的端点更改为此
controller.java
@PostMapping("/register")
public String postRegister(User user) {
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String encodedPassword = passwordEncoder.encode(user.getPassword());
user.setPassword(encodedPassword);
userService.save(user);
return "redirect:/";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.