Posted on 2007-04-11 10:40
HandSoft 閱讀(6402)
評論(0) 編輯 收藏 所屬分類:
開源學習
在Strut中,實現(xiàn)table中復制一行的功能
line[j]是要復制的一行,Action中可以獲取到要復制的行的ID.
因為line[j]中有很多屬性,要是一個一個的屬性去get,然后set的話,代碼量會
很大,而且會出現(xiàn)很多冗余代碼。
這是我要復制出來的一行
if (j == rowId && !line[j].getNewRecord()) {
rowList.add(line[j]);
//將這一行全部復制
}
現(xiàn)在要使得其中的某幾個屬性復制出來為空
則需要一個一個的set,get.
if (j == rowId && !line[j].getNewRecord()) {
CreateDeliveryLineRow cdlr = new CreateDeliveryLineRow ();
if(line[j].getMfgLot() != null){
cdlr.setMfgLot = null;
}
。。。。。。
rowList.add(cdlr);
//將這一行全部復制
}
以下是比較好的解決方案:
利用apache的common類中的BeanUtils來實現(xiàn)對象屬性的復制
if (j == rowId && !line[j].getNewRecord()) {
CreateDeliveryLineRow row = new CreateDeliveryLineRow();
BeanUtils.copyProperties(row,line[j]); //復制出對象line[j],將其屬性賦予row
row.setQuantity(null); //在row中輕松的實現(xiàn)對某幾個屬性的控制
row.setMfgLot(null);
row.setMiniQuantity(null);
row.setBoxQuantity(null);
rowList.add(row);
//rowList.add(cdr);
}
===================================================
CreateDeliveryForm getForm = (CreateDeliveryForm) form;
。。。。。。
CreateDeliveryLineRow[] line = getForm.getLine();
if (line != null && line instanceof CreateDeliveryLineRow[]) {
int size = line.length;
for (int j = 0; j < size; j++) {
if (!line[j].getNewRecord() && !line[j+1].getNewRecord()) {
if (line[j].getBoxQuantity() == 0L) {
line[j].setBoxQuantity(null);
}
if (line[j].getMiniQuantity() == 0L) {
line[j].setMiniQuantity(null);
}
if (line[j].getQuantity() == 0D) {
line[j].setQuantity(null);
}
rowList.add(line[j]);
}
if (j == rowId && !line[j].getNewRecord()) {
CreateDeliveryLineRow row = new CreateDeliveryLineRow();
BeanUtils.copyProperties(row,line[j]);
row.setQuantity(null);
row.setMfgLot(null);
row.setMiniQuantity(null);
row.setBoxQuantity(null);
rowList.add(row);
//rowList.add(cdr);
}
}
}
。。。。。。
request.setAttribute("results", rowList);