title: How to submit multiple Line Items in Struts 2?
body: <p>I do have an Purchase request form, which dynamically creates row for <code>ItemName</code>, <code>quantity</code>, <code>specification</code>, <code>unit</code> of <code>measurement</code>.</p>
  
  <p>I do have a one button called "Add item", which dynamically creates new rows.</p>
  
  <p>When I submit this form how do I get it into Action class in Struts2. </p>
  
  <p><strong>JSP:</strong></p>
  
  <pre class="lang-jsp prettyprint-override"><code>    &lt;table id="itemDetails" width="100%"&gt;
      &lt;tr  width="100%" cellpadding="0" cellspacing="0" border="1" &gt;
      &lt;th &gt;&lt;center&gt;+&lt;/center&gt;&lt;/th&gt;
      &lt;th &gt;Item&lt;/th&gt;
      &lt;th &gt;Specification&lt;/th&gt;
      &lt;th &gt;Quantity&lt;/th&gt;
      &lt;th &gt;Unit Of Measurement&lt;/th&gt;
      &lt;th &gt;Operations&lt;/th&gt;
      &lt;/tr&gt;
  
      &lt;s:iterator value="id" status="ctr" &gt;
       &lt;tr&gt;
        &lt;td  width="5%"&gt;&lt;input type="checkbox"  name="rdel"  &gt;&lt;br&gt;&lt;br&gt;&lt;/td&gt;
        &lt;td  width="30%" &gt;&lt;input  type="text" name="id[%{#ctr.index}].itemname" /&gt;&lt;/td&gt;
        &lt;td  width="30%"&gt;&lt;input type="text" name="id[%{#ctr.index}].specification"  &gt;&lt;/td&gt;                      
        &lt;td  width="20%"&gt;&lt;input type="text" name="id[%{#ctr.index}].quantity" &gt;&lt;/td&gt;
        &lt;td  width="5%"&gt;&lt;input type="text"  name="id[%{#ctr.index}].uom" &gt;&lt;/td&gt;
        &lt;td  width="10%"&gt;&lt;a href="#" &gt;Delete&lt;/a&gt;&lt;/td&gt;
       &lt;/tr&gt;
      &lt;/s:iterator&gt;
      &lt;/table&gt;
  </code></pre>
  
  <p>Javascript for dynamic row creation:</p>
  
  <pre class="lang-jsp prettyprint-override"><code>&lt;SCRIPT language="javascript"&gt;
  function addRow(itemDetails) {
  
      var table = document.getElementById(itemDetails);
  
      var rowCount = table.rows.length;
  
      var row = table.insertRow(rowCount);
      var counts=rowCount-1;
  
  
      var cell1 = row.insertCell(0);
      var check = document.createElement("input");
      check.type = "checkbox";
      check.name="rdel";
      cell1.appendChild(check);
  
      var cell2 = row.insertCell(1);
      var item = document.createElement("input");
      item.type = "text";
      item.name="id.item";
      cell2.appendChild(item);
  
      var cell3 = row.insertCell(2);
      var specification = document.createElement("input");
      specification.type = "text";
      specification.name="id.specification";
      cell3.appendChild(specification);
  
      var cell4 = row.insertCell(3);
      var quantity = document.createElement("input");
      quantity.type = "text";
      quantity.name="id.quantity";
      cell4.appendChild(quantity);
  
      var cell5 = row.insertCell(4);
      var uom = document.createElement("input");
      uom.type = "text";
      uom.name="id.uom";
      cell5.appendChild(uom);
  
      var cell6 = row.insertCell(5);
      var operations = document.createElement("a");
      operations.setAttribute("href","#");
      operations.innerText="Delete";
      cell6.appendChild(operations);
  
  
  }
  
  &lt;/SCRIPT&gt;
  </code></pre>
  
  <p>Action class method:</p>
  
  <pre class="lang-java prettyprint-override"><code> private List&lt;ItemDetails&gt; id;
   public List&lt;ItemDetails&gt; getId(){
  
      return this.id;
   }
  
   public void setId(List&lt;ItemDetails&gt; id) {
      this.id = id;
   }
  
   public String savePurchaseRequest(){
    try{
  
        setId(getId());   
  
     for(ItemDetails itemdetails:id ) {
         System.out.println( itemdetails.getItemname() + ":" + itemdetails.getSpecification() +":"+ itemdetails.getQuantity()+ ":"+ itemdetails.getUom() );
      }
  
      }catch(Exception e){
        System.out.println("Exception:"+e);
      }
  
    return SUCCESS;
   }
  </code></pre>
  
  <p>and <code>ItemDetails</code> class:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class ItemDetails implements java.io.Serializable {
  
  private String itemname;
  
  private String specification;
  
  private String quantity;
  
  private String uom;
  
  
  
  public ItemDetails() {
  
  }
  
  
  
  public ItemDetails(String itemname, String specification, String quantity, String uom) {
  
  this.itemname = itemname;
  
  this.specification = specification;
  
  this.quantity = quantity;
  
  this.uom = uom;
  
  }
  
  
  
  
  }
  
  
  
  public String getItemname() {
  
  return this.itemname;
  
  }
  
  
  
  public void setItemname(String itemname) {
  
  this.itemname = itemname;
  
  }
  
  
  
  public String getSpecification() {
  
  return this.specification;
  
  }
  
  
  
  public void setSpecification(String specification) {
  
  this.specification = specification;
  
  }
  
  
  
  public String getQuantity() {
  
  return this.quantity;
  
  }
  
  
  
  public void setQuantity(String quantity) {
  
  this.quantity = quantity;
  
  }
  
  
  
  public String getUom() {
  
  return this.uom;
  
  }
  
  
  
  public void setUom(String uom) {
  
  this.uom = uom;
  
  }
  
  
  
  
  }
  </code></pre>