Question: I'm encountering an issue with Thymeleaf while trying to bind a form field to an object in my Spring Boot application. I'm receiving the following error message:
org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring6.processor.SpringInputGeneralFieldTagProcessor' (template: "account-create" - line 22, col 54) ... java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'accountCreationRequestDto' available as request attribute ...
Here's the relevant code from my Thymeleaf template:
<form th:action="@{/create}" method="post" th:object="${accountCreationRequestDto}">
<label for="clientId">Client ID:</label>
<input type="text" id="clientId" name="clientId" th:field="*{clientId}" required>
<span th:if="${#fields.hasErrors('clientId')}" th:errors="*{clientId}"></span>
<!-- Other form fields... -->
<button type="submit">Create Account</button>
</form>
And here's the relevant code from my controller:
@GetMapping("/create")
public String showCreateAccountForm(Model model) {
model.addAttribute("accountCreationRequestDto", new AccountCreationRequestDto());
return "account-create";
}
@PostMapping("/create")
public String createAccount(@ModelAttribute("accountCreationRequestDto") AccountCreationRequestDto accountCreationRequestDto, Model model) {
// Process account creation
return "redirect:/success";
}
AccountCreationRequestDto object is passed to the model attribute in the showCreateAccountForm method. However, when submitting the form, I'm encountering the mentioned error. The form action in the Thymeleaf template looks matching the mapping of the createAccount method in the controller.
Any help in resolving this issue would be greatly appreciated. Thank you!