2013-04-28 75 views
0

我认为我的语法是正确的,但我的表单支持对象中的列表没有被填充。它看起来像列表本身正在制定,只是它没有元素。其他属性正在按预期填充。有任何想法吗?Spring 3绑定对象到列表

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 
+0

好吧,我想出了我做了什么改变,得到了这个工作。我使用GET请求上的Contact对象初始化列表。我说initalize,但我的意思是我只是添加一个元素到列表中,所以它不是空的。该列表本身已经在AddReminder类中初始化。 Spring将自动增加列表。 – 2013-04-28 22:09:36

回答

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

+0

对不起,我没有提到,名单是空的,这就是为什么我没有使用foreach。我试图从表单的信息中填充空列表,但是在做这件事时遇到了麻烦。 – 2013-04-28 18:42:01

+0

呃确定很奇怪。重新开始eclipse,现在我的列表正在填充。好吧,无论如何,感谢您的链接,很好地了解更多关于春天。 – 2013-04-28 20:57:21

+0

我不认为你有问题,如果清单是空的,但如果你遇到这个问题,请使用c:如果首先检查llistt的大小,如果0不执行c:foreach – 2013-04-28 22:11:58

0

好吧,我想通了什么样的变化我做出了这个工作。我使用GET请求上的Contact对象初始化列表。我说initalize,但我的意思是我只是添加一个元素到列表中,所以它不是空的。该列表本身已经在AddReminder类中初始化。 Spring将自动增加列表。