我每次保存的时候,就会把上一次在页面中填的也保存到了数据库,不知道为什么??比如,我第一次在页面填了5行数据,没什么问题,第二次我填了2行数据,保存后,hibernate做了5次保存操作。第二次保存的数据中,前2行填的数据,后3行是第一次保存中填的后三行的数据(这三行是不应该存在的数据,因为我只填了2行),这是为什么??
Action内容:
VoucherActionForm vform = (VoucherActionForm) form;
List list = vform.getVouchers();
Service service = ServiceFactory.getService();
System.out.println(list.size());
Voucher voucher = new Voucher();
int number = service.countNumber() + 1;
for (int i = 0; i < list.size(); i++) {
voucher = (Voucher) list.get(i);
service.addVoucher(voucher);
}
ActionForm内容:
private List vouchers = new AutoArrayList(Voucher.class);
public List getVouchers() {
return vouchers;
}
public void setVouchers(List vouchers) {
this.vouchers.clear();
this.vouchers.addAll(vouchers);
}
页面:
<script>
rowid = 0;
function insertRow(){
rowid = rowid + 1;
newRow=PLList.insertRow(PLList.rows.length);
newRow.id="tdt";
newRow.bgColor="#e0e0e0";
newRow.className="tableData";
c1=newRow.insertCell(0);
c1.id="delItem";
c1.className="scheduleButtonVisible";
c1.innerHTML="<input type=text name=vouchers[0].zy maxlength=50 size=50 style=border-style: none class=tableData >";
c2=newRow.insertCell(1);
str="<select name=vouchers[0].zzkm style=border-style: none><option selected></option><option>库存现金</option><option>银行存款</option><option>应收款</option><option>成员往来</option><option>产品物资</option><option>委托加工物资</option><option>委托代销商品</option><option>受托代购商品</option><option>受托代销商品</option><option>对外投资</option><option>牲畜(禽)资产</option><option>林木资产</option><option>固定资产</option><option>累计折旧</option><option>在建工程</option><option>固定资产清理</option><option>无形资产</option><option>短期借款</option><option>应付款</option><option>应付工资</option><option>应付盈余返还</option><option>应付剩余返还</option><option>长期借款</option><option>专项应付款</option><option>股金</option><option>专项基金</option><option>资本公积</option><option>盈余公积</option><option>本年盈余</option><option>盈余分配</option><option>生产成本</option><option>经营支出</option><option>管理费用</option><option>其他支出</option><option>经营收入</option><option>其他收入</option><option>投资收益</option></select>";
c2.innerHTML=str;
c3=newRow.insertCell(2);
c3.innerHTML="<input type=text name=vouchers[0].mxkm maxlength=50 style=border-style: none class=tableData >";
c4=newRow.insertCell(3);
c4.innerHTML="<input type=text name=vouchers[0].jf maxlength=11 size=10 style=border-style: none class=tableData >";
c5=newRow.insertCell(4);
c5.innerHTML="<input type=text name=vouchers[0].df maxlength=11 size=10 style=border-style: none class=tableData >";
}
//删除最后1行
function DeleteRow(tableId)
{ var objTable = document.getElementById(tableId);
if(objTable.rows.length==2)
{ alert("对不起,你不能删除表格头!!!");
return; }
if(confirm("确定删除?"))
{ objTable.deleteRow();
rowid = rowid - 1;
}
}
function resetIndex()
{
var inputs = document.getElementsByTagName("input");
for(var i=0;i<inputs.length;i++)
{
if(inputs[i].name.indexOf('vouchers[')!=-1)
{
var index =inputs[i].parentNode.parentNode.rowIndex;
inputs[i].name = inputs[i].name.replace(/\d+/,index-1);
}
}
var selects = document.getElementsByTagName("select");
for(var i=0;i<selects.length;i++){
if(selects[i].name.indexOf('vouchers[')!=-1)
{
var index =selects[i].parentNode.parentNode.rowIndex;
selects[i].name = selects[i].name.replace(/\d+/,index-1);
}
}
}
</script>
<form action="/Hzs/voucherAction.do" method="get">
<table align=center>
<tr align=center>
<td>
<%request.setAttribute("date",new Date()); %>
日 期:
<fmt:formatDate dateStyle="long" type="date" value="${date }"/>
第${number }号
<br>
</td>
</tr>
<tr>
<td>
<table id=PLList align=center border=0 >
<tr align=center bgColor="#e0e0e0">
<td>
摘 要
</td>
<td>
总账科目
</td>
<td>
明细科目
</td>
<td>
借方金额
</td>
<td>
贷方金额
</td>
</tr>
<tr align=center id=row1 bgColor="#e0e0e0">
<td>
<input type=text name="vouchers[0].zy" maxlength=50 size=50
style="border-style: none" />
</td>
<td>
<select name="vouchers[0].zzkm" style="border-style: none">
<option selected>
</option>
<option>
库存现金
</option>
... ... ... ...
<td>
<input type=text name="vouchers[0].jf" maxlength=11 size=10
style="border-style: none" />
</td>
<td>
<input type=text name="vouchers[0].df" maxlength=11 size=10
style="border-style: none" />
</td>
</tr>
</table>
</td>
</tr>
<tr align=center>
<td>
<div align=center>
<br>
<input type="submit" name="Submit2" value="确定保存" onclick="resetIndex()">
<input type="reset" name="Submit1" value="重新填写">
<input type="button" value="添加一行" onclick="insertRow()" />
<input type="button" onClick="DeleteRow('PLList')" value="删除一行">
</div>
</td>
</tr>
</table>
</form>
问题补充:
public class VoucherActionForm extends ActionForm {
private List vouchers = new AutoArrayList(Voucher.class);
每次都是一个新的List 呀
问题补充:
那我在Action里加了一行 list.clear(); 时就不会出现那种情况了。
这个就是你说的问题吗??