利用Strut2进行台帐维护时,网页上传的数据经常不包括台帐对象的所有属性。对于网页端上传对象不包含的属性,则对相应的后台对象属性不进行更新。
示例展示了通过判断网页端上传对象的属性值是否为空,如果为空,则不对相应的后台对象属性进行更新。
后台对象总共有8个属性:
只编辑了4个属性:
关键类代码如下:
package lian;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
/**
* 复制相似Row之间的数据,比如讲一个有数据的Row的所有数据选择性的复制到一个空的Row中,通过属性的set、get方法
*
* desRow和srcRow的类型是一样的
*
*
* 注意:栏位以VO中Attribute标签里的列表为准,desRow中存在的属性则srcRow中必须存在,
* 否则在needlessColumn_List加入srcRow中不存在的属性
*/
public class CloneUtil {
private Object desRow; // 目标Row
private Object srcRow; // 源Row
private List<String> needlessColumn_List; // 需要跳过的属性的List
public CloneUtil(Object desRow, Object srcRow,
List<String> needlessColumn_List) {
this.desRow = desRow;
this.srcRow = srcRow;
this.needlessColumn_List = needlessColumn_List;
}
public static boolean isNull(Object obj) {
return obj == null || "".equals(obj.toString())
|| "null".equalsIgnoreCase(obj.toString())
|| "undefined".equalsIgnoreCase(obj.toString())
|| "".equals(obj.toString().trim());
}
/**
* 主方法
*
* @throws Exception
*/
public void deepClone() {
Method setMethod = null; // 源 Bean中的属性的set方法
Method getMethod = null; // 源 Bean中的属性的get方法
Class desRowClass = desRow.getClass(); // 得到desRow的Class
// 所有Public范围的方法,包括从父类继承的和从接口实现的公共方法
Method[] publicMethods = desRowClass.getMethods();
// 本类定义的所有范围的方法,不包括从父类继承的和从接口实现的方法
Method[] exceptedSuperMethods = desRowClass.getDeclaredMethods();
List<Method> neededMethodsList = new ArrayList<Method>(); // 保存Bean有对应属性的方法的List
fetchNeededMethods(publicMethods, exceptedSuperMethods,
neededMethodsList); // 只将Bean的属性的set方法放入neededMethodsList中以便处理
try {
for (Method neededMethod : neededMethodsList) { // 循环处理此desRow的Class对应的所有方法
String neededMethodName = neededMethod.getName();
// 跳过不需要复制的属性
boolean inList = false;
if (this.needlessColumn_List != null
&& !needlessColumn_List.isEmpty()) {
inList = checkNeedlessColumn(neededMethodName); // 检查不需要复制的属性
if (inList == false) { // 如果当前属性名称存在于needlessColumn_List中,则跳过
continue;
}
}
// 得到源Bean中的属性的set方法
setMethod = srcRow.getClass().getMethod(neededMethodName,
neededMethod.getParameterTypes());
// 得到源Bean中的属性的get方法
getMethod = srcRow.getClass().getMethod(
convertName(setMethod.getName()), null);
Object item = getMethod.invoke(srcRow, null); // 执行源
// Bean的get方法,得到返回值
if (!isNull(item))
neededMethod.invoke(desRow, item); // 执行目标Bean的set方法
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 只将VO的属性的set方法放入neededMethodsList中以便处理
* 因为publicMethods和exceptedSuperMethods中可能有很多不需要方法
*
* @param publicMethods
* @param exceptedSuperMethods
* @param neededMethodsList
*/
private void fetchNeededMethods(Method[] publicMethods,
Method[] exceptedSuperMethods, List<Method> neededMethodsList) {
for (Method publicMethod : publicMethods) { // 遍历publicMethods
String publicMethodName = publicMethod.getName(); // publicMethods中的每一个方法名
if (publicMethodName.startsWith("set")) { // 只处理以“set”开头的方法
for (Method exceptedSuperMethod : exceptedSuperMethods) { // 遍历methodsExceptSuper
String exceptedSuperMethodName = exceptedSuperMethod
.getName();
// 如果publicMethodName与exceptedSuperMethodName相等
if (publicMethodName.equals(exceptedSuperMethodName))
neededMethodsList.add(publicMethod); // 将符合条件的方法放入此List中
}
}
}
}
/**
* 过滤needlessColumn_List中的条目
*
* @param methodName
* desRow的Class中以“set”开头的方法
* @return 如果methodName存在于此List中 则返回false
*/
private boolean checkNeedlessColumn(String methodName) {
ListIterator<String> it = needlessColumn_List.listIterator();
while (it.hasNext()) {
String needlessColumn = it.next();
String uuidCName = "set" + needlessColumn;
if (uuidCName.equals(methodName)) {
needlessColumn_List.remove(needlessColumn);
return false;
}
}
return true;
}
/**
* 将setXxx方法名称转换成getXxx方法名称
*
* @param methodName
* set开头的方法名称
* @return 对应的get方法
*/
private String convertName(String methodName) {
if (methodName == null) {
return null;
}
String s = methodName.substring(3, methodName.length());
return "get" + s;
}
}
Project压缩包见: HelloWorld