<预><代码>org.thymeleaf.exceptions.TemplateInputException:模板解析过程中发生错误(模板:类路径资源[模板/时间表/show.html]")在...引起:java.lang.IllegalStateException:Bean 名称editEntryForm"的 BindingResult 和普通目标对象均不可用作请求属性...2020-05-15 09:19:47.449 ERROR 10251 --- [nio-9090-exec-2] oaccC[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] 在上下文中路径[]抛出异常[请求处理失败;嵌套异常是 org.thymeleaf.exceptions.TemplateInputException:模板解析期间发生错误(模板:类路径资源 [模板/时间表/show.html]")] 与根本原因...java.lang.IllegalStateException:Bean 名称editEntryForm"的 BindingResult 和普通目标对象均不可用作请求属性在 org.springframework.web.servlet.support.BindStatus.
(BindStatus.java:153) ~[spring-webmvc-5.2.6.RELEASE.jar:5.2.6.RELEASE]在您是否发现我做错了什么,或者是我不知道 Thymleaf 的限制.
解决方案
就我而言,我最终要做的是制作一个 id 列表(例如 [1,2,3])并添加一堆表单到在名称中使用相同 id 的模型(p.exform_1"、form_2"、form_3").
在模板中,我使用 th:each="id : ${ids}"
遍历 id 列表,我的 th:object 看起来像这样
th:object="${__${'form_'+ id}__}"
这样 th:object 绑定到一个直接位于模型下方的值,然后我迭代访问它
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.
解决方案
In my case what i ended up doing, is making a list of ids (ex. [1,2,3]) and adding a bunch of form to the model using the same id in the name (p.ex "form_1", "form_2", "form_3").
In the template, i iterate over the list of ids with a th:each="id : ${ids}"
and my th:object looks like this
th:object="${__${'form_'+ id}__}"
This way the th:object bind to a value that is directly below the model and i iterate to access it
这篇关于将表单支持 bean 列表绑定到表单列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!