Hello, I have a question why the registration of a new user does not work? I write on java using spring boot and thymeleaf. Thanks a lot in advance.

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <title>Create an account</title> </head> <body> <form action="#" th:action="@{/hello}" th:object="${userForm}" method="post"> <table> <tr> <td>User name:</td> <td><input type="text" th:field="*{username}" /></td> <td th:if="${#fields.hasErrors('username')}" th:errors="*{username}">Name Error</td> </tr> <tr> <td>Password:</td> <td><input type="password" th:field="*{password}" /></td> <td th:if="${#fields.hasErrors('password')}" th:errors="*{password}">Password Error</td> </tr> <tr> <td>Confirm password:</td> <td><input type="password" th:field="*{passwordConfirm}" /></td> <td th:if="${#fields.hasErrors('passwordConfirm')}" th:errors="*{passwordConfirm}">passwordConfirm Error</td> </tr> <tr> <td><button type="submit">Submit</button></td> </tr> </table> </form> </body> </html> 

controller

 @RequestMapping(value = "/registration",method = RequestMethod.GET) public String registration(Model model){ model.addAttribute("userForm",new User()); return "registration"; } @RequestMapping(value = "/registration", method = RequestMethod.POST) public String registration(@ModelAttribute("userForm") User userForm, BindingResult bindingResult, Model model){ userValidator.validate((userForm),bindingResult); if (bindingResult.hasErrors()){ return "registration"; } userService.save(userForm); securityService.autologin(userForm.getUsername(), userForm.getPassword()); return "redirect:/home"; } 

and the validator himself

 @Component public class UserValidator implements Validator { @Autowired private UserService userService; @Override public boolean supports(Class<?> aClass) { return User.class.equals(aClass); } @Override public void validate(Object o, Errors errors) { User user = (User) o; ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "NotEmpty"); if (user.getUsername().length() < 6 || user.getUsername().length() > 32) { errors.rejectValue("username", "Size.userForm.username"); } if (userService.findByUsername(user.getUsername()) != null) { errors.rejectValue("username", "Duplicate.userForm.username"); } ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "NotEmpty"); if (user.getPassword().length() < 8 || user.getPassword().length() > 32) { errors.rejectValue("password", "Size.userForm.password"); } if (!user.getPasswordConfirm().equals(user.getPassword())) { errors.rejectValue("passwordConfirm", "Diff.userForm.passwordConfirm"); } } 

}

    2 answers 2

    Change

     <form action="#" th:action="@{/hello}" th:object="${userForm}" method="post"> 

    on

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

    In your case, you need the th:action values ​​in the form and value in the controller for the RequestMethod.POST method to match. Then the data from the form will go where it is needed.

      You either tore this code from somewhere nichrome without understanding what was there, or you don’t understand what you wrote at all. You do not have the correct mappers for the / hello + link to everything in the post request you are accepting an attendant that is not in general in the form