I am trying to create a page on Struts2 that adds values to a database table. The problem is that the page should allow users to enter multiple rows into the database table. When the user clicks the submit button, he must read and write rows to the database table. Users can add or remove rows from the page.
So I tried to display the List value on the page. Java code is as follows:
List<Testimate> estimates;
private String removeIndex;
public String execute() throws Exception {
estimates = new ArrayList<Testimate>();
for(int i = 0; i < INITIAL_ESTIMATES; i++)
estimates.add(new Testimate());
return INPUT;
}
public String add() {
estimates.add(new Testimate());
System.out.println(estimates.size());
return INPUT;
}
public String remove() {
estimates.remove(Integer.parseInt(getRemoveIndex()));
System.out.println(estimates.size() + " " + getRemoveIndex());
return INPUT;
}
And the page looks something like this:
<script>
setRemoveIndex()
{
$('input[name="removeIndex"]').val(removeIndex);
return true;
}
</script>
<s:form theme="custom" onsubmit="setRemoveIndex()">
<s:submit action="CEST02_add" cssClass="ButtonSmall" value="Add estimate" />
<s:hidden name="removeIndex"/>
<table>
<s:iterator value="estimates" var="estimate" status="status">
<tr>
<td><s:textfield name="estimates[%{#status.index}].name"cssClass="product" /></td>
<td><s:textfield name="estimates[%{#status.index}].price"cssClass="product" /></td>
<td><s:textfield name="estimates[%{#status.index}].date"cssClass="product" /></td>
<td><s:textfield name="estimates[%{#status.index}].attr"cssClass="product" /></td>
<td><s:submit action="CEST02_remove" cssClass="ButtonSmall" value="Remove this estimate" onclick="removeIndex=%{#status.index}"/>
</td>
</tr>
</s:iterator>
</table>
</s:form>
" ", "". . " ", "". , . , . .
, . , , . - , ,
UPDATE:
JSP. , .
<s:iterator value="estimates" var="estimate" status="status">
<s:if test="#status.index != removeIndex">