How to submit multiple Line Items in Struts 2?

2019-01-09 17:08发布

问题:

I do have an Purchase request form, which dynamically creates row for ItemName, quantity, specification, unit of measurement.

I do have a one button called "Add item", which dynamically creates new rows.

When I submit this form how do I get it into Action class in Struts2.

JSP:

    <table id="itemDetails" width="100%">
    <tr  width="100%" cellpadding="0" cellspacing="0" border="1" >
    <th ><center>+</center></th>
    <th >Item</th>
    <th >Specification</th>
    <th >Quantity</th>
    <th >Unit Of Measurement</th>
    <th >Operations</th>
    </tr>

    <s:iterator value="id" status="ctr" >
     <tr>
      <td  width="5%"><input type="checkbox"  name="rdel"  ><br><br></td>
      <td  width="30%" ><input  type="text" name="id[%{#ctr.index}].itemname" /></td>
      <td  width="30%"><input type="text" name="id[%{#ctr.index}].specification"  ></td>                      
      <td  width="20%"><input type="text" name="id[%{#ctr.index}].quantity" ></td>
      <td  width="5%"><input type="text"  name="id[%{#ctr.index}].uom" ></td>
      <td  width="10%"><a href="#" >Delete</a></td>
     </tr>
    </s:iterator>
    </table>

Javascript for dynamic row creation:

<SCRIPT language="javascript">
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);


}

</SCRIPT>

Action class method:

 private List<ItemDetails> id;
 public List<ItemDetails> getId(){

    return this.id;
 }

 public void setId(List<ItemDetails> 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;
 }

and ItemDetails class:

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;

}




}

回答1:

+1, you was almost there.

Just change the Javascript part where you assign the name attribute, including the index exactly as you do in the iteration:

item.name="id.item";
specification.name="id.specification";
// ecc...

must become

item.name="id["+counts+"].itemname";
specification.name="id["+counts+"].specification";

And you will be good.



回答2:

  1. It looks like you set your model items from another page. Try setting them to another variable than id, something like idOutput or so, I had problems myself before when trying to access a field via a getter when this field got set before, although I don't know why this was the case.

  2. The model object (ItemDetails) needs public getters ( like getItemname() ) , so the JSP can read them.



回答3:

You need to put correct names in the JavaScript, so that OGNL is able to handle them properly and apply values when you submit the form.

To submit newly added row you need to use only these names on the row you've being submitted.