Note that there are some explanatory texts on larger screens.

plurals
  1. POStruts2 dynamically add, remove list of objects from page
    primarykey
    data
    text
    <p>I am trying to build a page on Struts2 that adds values to a database table. The problem is, the page must allow users to input several rows to the database table. When user clicks submit, it must read and write rows to the database table. Users can add or remove rows from page</p> <p>Therefore I tried to render List value on page. Java code looks like this:</p> <pre><code>List&lt;Testimate&gt; estimates; private String removeIndex; public String execute() throws Exception { estimates = new ArrayList&lt;Testimate&gt;(); for(int i = 0; i &lt; 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; } </code></pre> <blockquote> <pre><code>And the page looks something like this: </code></pre> </blockquote> <pre><code>&lt;script&gt; setRemoveIndex() { $('input[name="removeIndex"]').val(removeIndex); return true; } &lt;/script&gt; &lt;s:form theme="custom" onsubmit="setRemoveIndex()"&gt; &lt;s:submit action="CEST02_add" cssClass="ButtonSmall" value="Add estimate" /&gt; &lt;s:hidden name="removeIndex"/&gt; &lt;table&gt; &lt;s:iterator value="estimates" var="estimate" status="status"&gt; &lt;tr&gt; &lt;td&gt;&lt;s:textfield name="estimates[%{#status.index}].name"cssClass="product" /&gt;&lt;/td&gt; &lt;td&gt;&lt;s:textfield name="estimates[%{#status.index}].price"cssClass="product" /&gt;&lt;/td&gt; &lt;td&gt;&lt;s:textfield name="estimates[%{#status.index}].date"cssClass="product" /&gt;&lt;/td&gt; &lt;td&gt;&lt;s:textfield name="estimates[%{#status.index}].attr"cssClass="product" /&gt;&lt;/td&gt; &lt;td&gt;&lt;s:submit action="CEST02_remove" cssClass="ButtonSmall" value="Remove this estimate" onclick="removeIndex=%{#status.index}"/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/s:iterator&gt; &lt;/table&gt; &lt;/s:form&gt; </code></pre> <p>And when I click "Add estimate" it adds element to the list "estimates". And it prints the size correctly. But when I click "Remove this estimate", it does not change the list "estimates". But it prints that the size of list is reduced by one. And when I click again, the size does not change at all. It does not get any modification.</p> <p>Can you please tell me what is wrong with this code. I may have some huge misunderstanding how this framework works. If you have any questions or clarifications, please just ask</p> <p><strong>UPDATE:</strong></p> <p>I solved my issue with following lines on my JSP. But question still remains why I could not do this on my Action.</p> <pre><code>&lt;s:iterator value="estimates" var="estimate" status="status"&gt; &lt;s:if test="#status.index != removeIndex"&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload