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;
}
}