我想使用 ModelDriven 将收据列表添加到数据库中。这是行动
public class AddReceipAction extends ActionSupport implements ModelDriven<List<Receiptitem>>{
private List<Receiptitem> receiptItemList;
public List<Receiptitem> getReceiptItemList() {
return receiptItemList;
}
/**
* @param receiptItemList the receiptItemList to set
*/
public void setReceiptItemList(List<Receiptitem> receiptItemList) {
this.receiptItemList = receiptItemList;
}
@Override
public List<Receiptitem> getModel() {
return receiptItemList; //To change body of generated methods, choose Tools | Templates.
}
@Action(value="addreceipt",
results = {
@Result(name="success", location="/w_receipting.jsp")
})
public String Addreceipt()
{
List<Receiptitem> temp = new ArrayList<Receiptitem>();
for (Receiptitem item : receiptItemList)
{
if(item != null)
temp.add(item);
}
ReceipDAO receipDAO = new ReceipDAO();
receipDAO.add(temp);
return SUCCESS;
}
}
这是 jsp 页面:文本字段的名称是 Receipitem 对象的属性
<tr>
<td><s:select id="product1" name="receiptItemList[0].product.idProduct" label="Category" cssStyle="width: 178px" list="allProducts" /></td>
<td><s:textfield id="quan1" name="receiptItemList[0].receiptQuantity" label="Category Name" size="30" /></td>
<td><s:textfield id="price1" name="receiptItemList[0].receiptPrice" label="Discription" size="30" /> </td>
<td><s:textfield id="total1" value="" readonly="true"/></td>
</tr>
<tr>
<td><s:select id="product2" name="receiptItemList[1].product.idProduct" label="Category" cssStyle="width: 178px" list="allProducts" /></td>
<td><s:textfield id="quan2" name="receiptItemList[1].receiptQuantity" label="Category Name" size="30" /></td>
<td><s:textfield id="price2" name="receiptItemList[1].receiptPrice" label="Discription" size="30" /> </td>
<td><s:textfield id="total2" value="" readonly="true"/></td>
</tr>
<tr>
<td><s:select id="product3" name="receiptItemList[2].product.idProduct" label="Category" cssStyle="width: 178px" list="allProducts" /></td>
<td><s:textfield id="quan3" name="receiptItemList[2].receiptQuantity" label="Category Name" size="30" /></td>
<td><s:textfield id="price3" name="receiptItemList[2].receiptPrice" label="Discription" size="30" /> </td>
<td><s:textfield id="total3" value="" readonly="true"/></td>
</tr>
<tr>
<td><s:select id="product4" name="receiptItemList[3].product.idProduct" label="Category" cssStyle="width: 178px" list="allProducts" /></td>
<td><s:textfield id="quan4" name="receiptItemList[3].receiptQuantity" label="Category Name" size="30" /></td>
<td><s:textfield id="price4" name="receiptItemList[3].receiptPrice" label="Discription" size="30" /> </td>
<td><s:textfield id="total4" value="" readonly="true"/></td>
</tr>
<tr>
<td><s:select id="product5" name="receiptItemList[4].product.idProduct" label="Category" cssStyle="width: 178px" list="allProducts" /></td>
<td><s:textfield id="quan5" name="receiptItemList[4].receiptQuantity" label="Category Name" size="30" /></td>
<td><s:textfield id="price5" name="receiptItemList[4].receiptPrice" label="Discription" size="30" /> </td>
<td><s:textfield id="total5" value="" readonly="true"/></td>
</tr>
<tr>
<td><s:select id="product6" name="receiptItemList[5].product.idProduct" label="Category" cssStyle="width: 178px" list="allProducts" /></td>
<td><s:textfield id="quan6" name="receiptItemList[5].receiptQuantity" label="Category Name" size="30" /></td>
<td><s:textfield id="price6" name="receiptItemList[5].receiptPrice" label="Discription" size="30" /> </td>
<td><s:textfield id="total6" value="" readonly="true"/></td>
</tr>
<tr>
<td><s:select id="product7" name="receiptItemList[6].product.idProduct" label="Category" cssStyle="width: 178px" list="allProducts" /></td>
<td><s:textfield id="quan7" name="receiptItemList[6].receiptQuantity" label="Category Name" size="30" /></td>
<td><s:textfield id="price7" name="receiptItemList[6].receiptPrice" label="Discription" size="30" /> </td>
<td><s:textfield id="total7" value="" readonly="true"/></td>
</tr>
<tr>
<td><s:select id="product8" name="receiptItemList[7].product.idProduct" label="Category" cssStyle="width: 178px" list="allProducts" /></td>
<td><s:textfield id="quan8" name="receiptItemList[7].receiptQuantity" label="Category Name" size="30" /></td>
<td><s:textfield id="price8" name="receiptItemList[7].receiptPrice" label="Discription" size="30" /> </td>
<td><s:textfield id="total9" value="" readonly="true"/></td>
</tr>
<tr>
<td><s:select id="product9" name="receiptItemList[8].product.idProduct" label="Category" cssStyle="width: 178px" list="allProducts" /></td>
<td><s:textfield id="quan9" name="receiptItemList[8].receiptQuantity" label="Category Name" size="30" /></td>
<td><s:textfield id="price9" name="receiptItemList[8].receiptPrice" label="Discription" size="30" /> </td>
<td><s:textfield id="total9" value="" readonly="true"/></td>
</tr>
<tr>
<td><s:select id="product10" name="receiptItemList[9].product.idProduct" label="Category" cssStyle="width: 178px" list="allProducts" /></td>
<td><s:textfield id="quan10" name="receiptItemList[9].receiptQuantity" label="Category Name" size="30" /></td>
<td><s:textfield id="price10" name="receiptItemList[9].receiptPrice" label="Discription" size="30" /> </td>
<td><s:textfield id="total10" value="" readonly="true"/></td>
但它不起作用,receiptItemList 中的所有 Receiptitem 都为空。