I have a list of time entry for which i want to create a modal for in my page. Each time entry will have it's own modal. Inside of these modal, i want to put a form. Each form must point to one of the time entry backing bean.
Here is the relevant part of the endpoint attach to this page
@GetMapping("/time/{year}/{month}/{dayOfTheMonth}")
public String show(
ModelMap model,
@PathVariable Integer year,
@PathVariable Integer month,
@PathVariable Integer dayOfTheMonth
){
....
var editEntryForms = entries
.stream()
.map(EditEntryForm::new)
.collect(Collectors.toList());
model.addAttribute("editEntryForms", editEntryForms);
return "timesheet/show";
}
My form backing object
@Data
class EditEntryForm {
public EditEntryForm(TimeEntry timeEntry){
id = timeEntry.getId();
description = timeEntry.getDescription();
}
private Long id;
private String description;
}
And the (relevant parts of the) template
<div class="ui modal"
th:each="editEntryForm : ${editEntryForms}"
th:id="${'edit-entry-modal-'+editEntryForm.id}">
<div class="header">
Edit time entry
</div>
<div class="content">
<form class="ui form"
th:object="${editEntryForm}"
th:classappend="${#fields.hasErrors('*')} ? error"
th:id="${'edit-entry-form'+ editEntryForm.id}"
th:action="@{/time/{year}/{month}/{day}/{entryId}(year=${year}, month=${month}, day=${dayOfTheMonth}, entryId=${editEntryForm.id})}"
method="POST">
...
</form>
</div>
<div class="actions">
<button class="ui approve primary button" form="add-entry-form">Update entry</button>
<div class="ui cancel button">Cancel</div>
<div class="ui right floated basic button">
Delete
</div>
</div>
</div>
The form is visible in the resulting page, with the correct id (as requested by th:id="${'edit-entry-modal-'+editEntryForm.id}"
), so i assume the that my binding is correct.
But the template evaluation can't be completed, I have the following error
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/timesheet/show.html]")
at
...
Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'editEntryForm' available as request attribute
...
2020-05-15 09:19:47.449 ERROR 10251 --- [nio-9090-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/timesheet/show.html]")] with root cause
...
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'editEntryForm' available as request attribute
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:153) ~[spring-webmvc-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at
Do you see something I'm doing wrong, or maybe it's a limitation of Thymleaf that I'm not aware of.