Spring 3绑定对象列表

时间:2013-04-28 04:26:28

标签: spring

我认为我的语法是正确的,但我的表格支持对象中的列表没有填充。它看起来像列表本身,只是它没有元素。正在按预期填充其他属性。有什么想法吗?

JSP:

<form:form method="post" commandName="addReminder">
    Reminder Name <input type='text' name='reminderName' placeholder="Reminder Name"> <br />
    Date          <input type='text' name='date' placeholder="1/05/2013"> <br />
    Time          <input type='text' name='time' placeholder="4:00 PM"> <br />
    Time Zone     <input type='text' name='timeZone' placeholder="EDT"> <br />
    <br />
    Contacts    <input type='text' path="contacts[0].phoneNumber" placeholder="Name"> <br />
                <input type='text' path="contacts[1].phoneNumber" placeholder="Name"> <br />

                <input type="submit" value = "Add Reminder">
</form:form>

控制器:

    @RequestMapping(value = "/AddAReminder", method = RequestMethod.POST)
public String addReminder(@ModelAttribute("addReminder") AddReminder reminder, BindingResult result)
{
            //does stuff with the data from the form backing object

    return "Add A Reminder";
}

表格支持对象:

public class AddReminder
{
private String reminderName;
private String date;
private String time;
private String timeZone;
private ArrayList<Contact> contacts = new ArrayList<Contact>();
private String sentFrom;
private String message;
private String provider;

//getters and setters

联络对象:

public class Contact
{
private String firstName;
private String lastName;
private String phoneNumber;
private String provider;

//getters and setters

2 个答案:

答案 0 :(得分:0)

您可以将<c:forEach>用作:

<form:form method="post" commandName="allProductEdit">
   <c:forEach items="${allProductEdit.products}" var="prod" varStatus="pStatus">
      <form:input path="products[${pStatus.index}].description" />
      <form:input path="products[${pStatus.index}].price" />
   </c:forEach>
   <input type="submit" value="Execute">
</form:form>

点击此论坛链接http://forum.springsource.org/showthread.php?54509-lt-form-input-gt-inside-lt-c-forEach-gt

答案 1 :(得分:0)

好的,我知道我做了什么改变才能让它发挥作用。我在GET请求上使用Contact对象初始化列表。我说initalize,但我的意思是我只是在列表中添加一个元素,所以它不是空的。列表本身已在AddReminder类中初始化。然后Spring将自动增加列表。