Spring 3.1表单和列表绑定 [英] Spring 3.1 Form and List Binding

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

问题描述

搜索之后,我还无法找到解决方案.

I have not been able to find a solution on this yet after searching and searching.

使用Spring 3.1

Using Spring 3.1

我有一个表格,希望用户能够添加多点联系人(POC).在我的JSP中,我有一个提交按钮,用于添加poc.它进入一个Spring Controller,从域对象获取pocs的当前列表(最初将是空的),将poc添加到列表中,然后将域对象放回到模型中,然后返回相同的视图.然后,用户最终将提交整个页面,该页面将进入控制器,并将整个对象保存为持久性.

I have a form where I would like the user to be able to add multiple point of contacts (poc). In my JSP I have a submit button for adding a poc. It goes to a Spring Controller, gets the current list of pocs from the domain object (which will be empty at first), adds the poc to the list and then puts the domain object back into the model before returning to the same view. Then the user will ultimately submit the entire page which will go to the controller and save the entire object to persistence.

我尝试了各种尝试,并得出了不同的结果,从只能添加一个poc到覆盖现有的新poc,到无法在表单上显示输入的poc.我将输入当前拥有的代码.

I have tried various attempts and come up with different results from only being able to add one poc with a new one overwriting the existing, to not being able to get the entered poc displayed on the form. I will put the code I currently have.

JSP:

<form:form method="post" commandName="request">

   ...

   <h2>POC:</h2>
   <input type="text" name="newPoc"/>
   <input type="submit" name="addPoc" value="Add POC"/>

   <table>
        ...
        <c:forEach items="${request.pointOfContacts}" var="poc" varStatus="vs">
             <tr>
                 <td><form:label path="pointOfContacts[${vs.index}].name/></td>
                  .....
        </c:forEach>

        ......

    </table>

</form:form>

Spring Controller:

Spring Controller:

@RequestMapping(value="/request", method=RequestMethod.POST, param="addPoc")
public Sring addPoc(@RequestParam String newPoc, MyRequest req, Model model) {
   PointOfContact poc = new PointOfContact();
   poc.setName(newPoc);
   List<PointOfContact> pocs = req.getPointOfContacts();
   pocs.add(poc);
   req.setPointOfContacts(pocs);
   model.addAttribute("request", req);
   return "requestForm";
}

域对象

@Entity
public class MyRequest {
   ...

   @OneToMany(Cascade=CascadeType.ALL)
   private List<PointOfContact> pointOfContacts = new ArrayList<PointOfContact>();

   .....
}

@Entity
public class PointOfContact {
   ...

   private String name;

  ....
}

有人有什么解决办法吗?我看过有关AutoPopulatingList的各种帖子,这是一种解决方案吗?如果是这样,我将如何在本示例中使用它.

Does anybody have any solutions? I have seen various posts about AutoPopulatingList, is this a solution? If so how would I use it in this example.

推荐答案

是的,您应该使用AutoPopulatingList,示例.这将需要更改MyRequest实体.

Yes you should use AutoPopulatingList, example. This will require change in MyRequest entity.

这篇关于Spring 3.1表单和列表绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆