2013-03-07 60 views
2

我确实有一个采购申请表,它可动态创建ItemNamequantity,specification,unitmeasurement的行。如何在Struts 2中提交多个行项目?

我确实有一个名为“Add item”的按钮,它动态地创建了新的行。

当我提交这个表单时,我如何在Struts2中将它加入Action类。

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进行动态记录创造:

<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类方法:

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

ItemDetails类:

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; 

} 




} 

回答

2

+1,你几乎在那里。

只要改变,你分配name属性JavaScript部分,包括完全按照你的迭代做索引:

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

必须成为

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

而且你会好的。

+0

一流的......它解决了..非常感谢您的帮助..安德里亚 – 2013-03-07 09:48:35

0
  1. 它看起来像你从另一个页面设置你的模型项目。尝试将它们设置为另一个变量,而不是id,类似于idOutput等等,当我试图通过getter访问一个字段之前,当这个字段被设置之前,我遇到了问题,尽管我不知道为什么会出现这种情况。

  2. 模型对象(ItemDetails)需要公共getter(如getItemname()),所以JSP可以读取它们。

+0

Model对象(ItemDetails)具有公共getter。他们与for循环的问题。它打印每行3次和getItemname()值不打印。 – 2013-03-07 09:12:36

+0

现在getItemname()通过在javascript中进行更改显示值。 – 2013-03-07 09:27:52

0

您需要在JavaScript中输入正确的名称,以便OGNL能够正确处理它们并在提交表单时应用值。

要提交新添加的行,您只需要在提交的行上使用这些名称。

+0

实际上我使用java脚本来从外部提交表单。并为您提供有关javascript名称的建议。 – 2013-03-07 09:56:28