I’m working on a registration form in Spring Boot
and thymeleaf but it’s not submitting to the database. 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); }
Advertisement
Answer
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.
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:/"; }