jsp - Collect and save submitted values of multiple dynamic HTML inputs back in servlet -
i able display arraylist
of beans in jsp form using jstl looping through list , outputting bean properties in html input tag.
<c:foreach items="${listofbeans}" var="bean"> <tr> <td><input type="text" id="foo" value="${bean.foo}"/></td> <td><input type="text" id="bar" value="${bean.bar}"/></td> </tr> </c:foreach>
how code jsp when page submits updated values in appropriate item of the arraylist
?
given simplified model:
public class item { private long id; private string foo; private string bar; // ... }
here's how can provided ${items}
list<item>
:
<c:foreach items="${items}" var="item"> <tr> <td> <input type="hidden" name="id" value="${item.id}" /> <input name="foo_${item.id}" value="${fn:escapexml(item.foo)}" /> </td> <td> <input name="bar_${item.id}" value="${fn:escapexml(item.bar)}" /> </td> </tr> </c:foreach>
(note importance of fn:escapexml() xss attack prevention)
so, basically, need set item's unique identifier hidden input field in each row shown in above snippet:
<input type="hidden" name="id" value="${item.id}" />
and should in turn use id
suffix of name
of input fields in same row such as:
<input name="foo_${item.id}" ... />
in servlet, can collect values of <input type="hidden" name="id" ...>
rows request.getparametervalues()
. loop on , grab individual inputs id
.
for (string id : request.getparametervalues("id")) { string foo = request.getparameter("foo_" + id); string bar = request.getparameter("bar_" + id); // ... }
you can without id
, grab inputs name array name="foo"
, request.getparametervalues("foo")
, ordering of request parameters not under control. html forms send in order, enduser can manipulate order.
no need javascript mess here.
Comments
Post a Comment