将表单支持 bean 列表绑定到表单列表 [英] Binding a list of form backing beans to a list of form

查看:35
本文介绍了将表单支持 bean 列表绑定到表单列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个时间条目列表,我想在我的页面中为其创建模态.每次输入都会有它自己的模态.在这些模态里面,我想放一个表格.每个表单必须指向一个时间条目的支持 bean.

这是附加到此页面的端点的相关部分

@GetMapping("/time/{year}/{month}/{dayOfTheMonth}")公共字符串显示(模型映射模型,@PathVariable 整数年,@PathVariable 整数月份,@PathVariable 整数 dayOfTheMonth){....var editEntryForms = 条目.stream().map(EditEntryForm::new).collect(Collectors.toList());model.addAttribute("editEntryForms", editEntryForms);返回时间表/节目";}

我的表单支持对象

@Data类 EditEntryForm {公共 EditEntryForm(TimeEntry timeEntry){id = timeEntry.getId();description = timeEntry.getDescription();}私人长ID;私人字符串描述;}

以及(相关部分)模板

<div class="ui modal"th:each="editEntryForm : ${editEntryForms}"th:id="${'edit-entry-modal-'+editEntryForm.id}"><div class="header">编辑时间条目

<div class="内容"><form class="ui 表单"th:object="${editEntryForm}"th:classappend="${#fields.hasErrors('*')} ? 错误"th:id="${'edit-entry-form'+ editEntryForm.id}"th:action="@{/time/{year}/{month}/{day}/{entryId}(year=${year}, month=${month}, day=${dayOfTheMonth}, entryId=${editEntryForm.id})}"方法=POST">...</表单>

<div class="actions"><button class="ui批准主按钮" form="add-entry-form">更新条目</button><div class="ui 取消按钮">取消</div><div class="ui 右浮动基本按钮">删除

该表单在结果页面中可见,具有正确的 id(按照 th:id="${'edit-entry-modal-'+editEntryForm.id}" 的要求),所以我假设我的绑定是正确的.

但是模板评估无法完成,出现如下错误

<预><代码>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屋!

查看全文
相关文章
Java开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆