Struts2 dynamically adds, removes a list of objects from a page

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">
+5
3

. Testimate, List<Testimate> estimates List<String> estimates;. .

List<String> estimates;
private int removeIndex;
//there getters and setters

public String execute() throws Exception
    {

        estimates = new ArrayList<String>();
        for(int i = 0; i < 5; i++){
            estimates.add(""+i);
        }
        return SUCCESS;
    }

    public String remove() {
        estimates.remove(getRemoveIndex());
        System.out.println(estimates.size() + " " + getRemoveIndex());
        return SUCCESS;
        }

JSP

<body>
  <script>
  function setRemoveIndex(val)
   {    
       alert(val);
       document.getElementById("removeIndex").value=val;
       //$('input[name="removeIndex"]').val(removeIndex);
       document.myform.action="remove.action";
       document.myform.submit();
        return true;
   }
</script>
<s:form theme="simple" id="myform" name="myform">
<s:submit action="CEST02_add" cssClass="ButtonSmall" value="Add estimate" />
<s:hidden name="removeIndex" id="removeIndex"/>
<table>
<s:iterator value="estimates" var="estimate" status="status">
<tr>
   <td><s:textfield name="estimates[%{#status.index}]"cssClass="product" /></td>
   <td>
   <s:submit action="remove" cssClass="ButtonSmall" value="Remove this estimate" onclick="return setRemoveIndex('%{#status.index}')"/>
   </td>
</tr>
</s:iterator>
</table>
</s:form>
</body>

Struts.xml

 <action name="remove" class="example.HelloWorld" method="remove">
     <result>/example/HelloWorld.jsp</result>
 </action>

List, , .

+3

Hibernate remove() ? , .

+2

, , , :

package com.mycompany;

import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author james
 */
public class EstimateAction extends ActionSupport {

    private List<Testimate> estimates;
    private String removeIndex;
    private static final int INITIAL_ESTIMATES = 10;

    public List<Testimate> getEstimates() {
        return estimates;
    }

    public void setEstimates(List<Testimate> estimates) {
        this.estimates = estimates;
    }

    public String getRemoveIndex() {
        return removeIndex;
    }

    public void setRemoveIndex(String removeIndex) {
        this.removeIndex = 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;
    }
}

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <!-- Configuration for the default package. -->
    <constant name="struts.devMode" value="true" />
    <package name="default" extends="struts-default">
        <action name="showEstimates" class="com.mycompany.EstimateAction">
            <result name="input">/estimatePage.jsp</result>
        </action>
        <action name="removeEstimates" class="com.mycompany.EstimateAction" method="remove">
            <result name="input">/estimatePage.jsp</result>
        </action>
    </package>
</struts>

estimatePage.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
        <script type="text/javascript">
            function setRemoveIndex(removeIndex)
            {
                document.myForm.action = "removeEstimates.action";    //First set the form to navigate to the specific action
                $('input[name="removeIndex"]').val(removeIndex);
                //alert("Here");
                document.myForm.submit();                             //Submit the form with the action attribute change
                return false;                                         //Cancel the form submission which was triggered earlier
            }
        </script>
    </head>
    <body>
        <s:form name="myForm" onsubmit="setRemoveIndex()">
            <s:submit action="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="remove" cssClass="ButtonSmall" value="Remove this estimate" onclick="return setRemoveIndex(%{#status.index})"/>
                        </td>
                    </tr>
                </s:iterator>
            </table>
        </s:form>
    </body>
</html>
+1

All Articles