0

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

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
4

2 回答 2

0

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

于 2013-04-28T22:11:57.290 回答
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

于 2013-04-28T08:51:50.513 回答