I've been tinkering with Thymeleaf recently a little bit and I'm trying to setup a basic registration form. I have set up my @GetMapping
and @Postmapping
controller handlers.
Controller:
@GetMapping("/registration")
public String userResponseEntity(Model model) {
UserModel userModel = new UserModel();
model.addAttribute("userModel", userModel);
return "registration";
}
@PostMapping("/registration")
public String userRegistration(@Valid @ModelAttribute("userModel") UserModel user, BindingResult result){
UserModel userToSubmit = new UserModel();
userToSubmit.setUsername(user.getUsername());
userToSubmit.setFirstName(user.getFirstName());
userToSubmit.setLastName(user.getLastName());
try {
User userReturned = userService.saveUser(userToSubmit).orElseThrow(
() -> new UserException(HttpStatus.BAD_REQUEST, "Unable to register user with given inputs"));
}catch (UserException e){
if(result.hasErrors()) {
logger.error("Could not save the given user to the database");
return "registration?error=true";
}
}
logger.info(String.format("User: %s has been registered successfully", userToSubmit.getUsername()));
return "registration?user-created=true";
}
The error I have been getting occurs when I click on a button that is not shown in the controller to minimizing purposes, but has the following href from a button located at /login
<p>Not a member? <a th:href="@{/registration}" >Register</a></p>
After I click on that I am not even redirected to /registration
, but instead prompted with the following error:
2022-12-21T19:07:11.241-06:00 ERROR 2109 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring6.processor.SpringInputGeneralFieldTagProcessor' (template: "registration" - line 22, col 94)] with root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'userDTO' available as request attribute
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:153) ~[spring-webmvc-6.0.2.jar:6.0.2]
The Thymeleaf template:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
<title>Login</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
crossorigin="anonymous"/>
</head>
<body>
<div class="container-fluid text-left">
<div class="row justify-content-md-center mt-3">
<div class="col-4">
<h3 class="fw-normal mb-3 pb-3 text-center" style="letter-spacing: 1px;">Register Account</h3>
<div th:if="${param.error}">//fields has errors
<p class="text-danger">[[${session.SPRING_SECURITY_LAST_EXCEPTION.message}]]</p>
</div>
<form th:action="@{/registration}" method="post" th:object="${userModel}">
<div class="form-group">
<label for="exampleInputEmail1" class="text-left">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" th:field="*{username}" aria-describedby="emailHelp" placeholder="Enter email">
</div>
<div class="form-group">
<label for="InputPassword" class="text-left">Password</label>
<input type="password" class="form-control" th:field="*{password}" id="InputPassword" placeholder="Password">
<label for="ReenterPassword" class="text-left">Re-enter Password</label>
<input type="password" class="form-control" th:field="*{password}" id="ReenterPassword" placeholder="Enter password again">
</div>
<div class="form-group form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">Check me out</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</body>
</html>