Shikha Dhawan
Shikha Dhawan

Reputation: 1424

How can I get a collection of values from a form?

I have the following ArrayList in my Action Class :

List<Student> students = ArrayList<Students>();

I have also created the associated setters & getters.

JSP snippet:

<s:form action="updateStudent">
 <s:iterator value="students ">
  <tr>
    <td><s:property value="name" /></td>
    <td><s:property value="address" /></td>
    <td><s:property value="age" /></td>
    <td><s:textfield name="mobile" /></td>  
  </tr>
 </s:iterator>
</s:form>

Displaying the information on JSP after fetching the information from the database works fine.

I need to edit the mobile & push back the same to database. But when I submit the form, the ArrayList "students" is nullified.

How can I push the information back to database. Not able to persist.

Upvotes: 1

Views: 950

Answers (1)

Dave Newton
Dave Newton

Reputation: 160191

Use array notation in the form:

<s:form action="updateStudent">
  <s:iterator value="students" status="stat">
    <s:textfield name="students.mobile[%{#status.index}]" />
  </s:iterator>
</s:form>

You shouldn't need to explicitly create a list in the action, but if you do, make sure the creation happens before the parameter interceptor tries to fill its values otherwise you'll overwrite the list.

Upvotes: 1

Related Questions