繁体   English   中英

Map Model 属性的动态输入列表 Spring MVC 来自 Z2D7FCA4F5778E14B40B496BB21

[英]Map a dynamic list of inputs to Model Attribute in Spring MVC from JSP

与这里的问题类似: List<Foo> as form backing object using Spring 3 MVC,正确的语法?

@RequestMapping(value = "/FOO", method = RequestMethod.POST)
public String getSendEmail(List<Foo> foos, Model model) {
    // ...
}

<form:form id="frmFoo" modelAttribute="foos">
   <c:forEach items="${foos}" var="foo">
     <form:input path="${foo.name}" type="text"/>

发布表单时,我需要能够将列表作为 model 属性。 包括上述示例在内的所有示例都假定列表中存在现有条目。 但是,如果一个人正在填写新表格,情况就不会如此。

所以我的问题是,当您登陆页面时不存在列表条目时,如何动态发布 model 属性列表。 如何动态创建将 map 输入到 null 或我的 model 属性的空列表的输入。

我想到了! 假设您有一个 model(为简洁起见,省略了获取器/设置器)

class SomeModel {
    List<SomeModelChildClass> children;
}

class SomeModelChildClass {
    String someString;
}

在您的 JSP 中,您可以像这样对孩子们做 map(即使它是空白的)......

这将 map 一个 SomeModelChildClass with someString = "I Did it." 当您发布到 controller 时。 这当然假设您在 SomeModel 的 controller 中有一个表格和 model 属性设置。 然后您可以通过输入发布,它将使用列表填充您的 model!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM