`
zhengpingling
  • 浏览: 13379 次
  • 性别: Icon_minigender_2
  • 来自: 深圳
社区版块
存档分类
最新评论

ActionFormUtil

阅读更多
package com.lxit.company.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import com.lxit.company.action.GoodsAction;

public class ActionFormUtil {
private Logger logger = Logger.getLogger(GoodsAction.class);
private static ActionFormUtil instance = null;

public ActionFormUtil() {

}

public static synchronized ActionFormUtil getInstance() {
if (null == instance) {
instance = new ActionFormUtil();
}
return instance;
}
/**
*
* 〈得到ActionForm节点〉
* @param [actionFormKey]     [key值]
* @return  [Object]
* @Author Erin
*/
public Object getActionForm(String actionFormKey) {
logger.debug(StringUtil.append("getActionForm", Common.BEGIN));
ConfigUtil configUtil = new ConfigUtil();
// action节点对应的form名
String form = configUtil.getActionFormName(actionFormKey);

// ActionForm类路径
String actionForm = configUtil.getActionFormValue(form);

Object object = null;
try {
object = Class.forName(actionForm).newInstance();
} catch (InstantiationException e) {
logger.error(StringUtil.append("getActionForm", e.getMessage()));
} catch (IllegalAccessException e) {
logger.error(StringUtil.append("getActionForm", e.getMessage()));
} catch (ClassNotFoundException e) {
logger.error(StringUtil.append("getActionForm", e.getMessage()));
}
logger.debug(StringUtil.append("getActionForm", Common.END));
return object;
}

/**
*
* 〈map集合到object映射〉
*
* @param [map] [Map集合]
* @param [object] [Object对象]
* @return [Object]
* @Author Erin
*/
public Object mapToObject(Map map, Object object) {
logger.debug(StringUtil.append("mapToObject", Common.BEGIN));
// 表单key集合
Set keySet = map.keySet();
// 对象map集合
Map fieldsMap = getFields(object);
// 遍历key集合
for (Object object2 : keySet) {
// 判断表单map集合是否包含字段
if (fieldsMap.containsKey(object2)) {
Method[] methods = object.getClass().getMethods();
for (Method method : methods) {
// 方法名
String name = method.getName();
// 判断值匹配
if (name.startsWith("set")
&& object2.equals(name.substring(3, 4)
.toLowerCase() + name.substring(4))) {
try {
// 返回数组
String[] str = (String[]) map.get(object2);
// 对象赋值
method.invoke(object, str[0]);
} catch (IllegalArgumentException e) {
logger.error(StringUtil.append("mapToObject", e.getMessage()));
} catch (IllegalAccessException e) {
logger.error(StringUtil.append("mapToObject", e.getMessage()));
} catch (InvocationTargetException e) {
logger.error(StringUtil.append("mapToObject", e.getMessage()));
}
}
}
}
}
logger.debug(StringUtil.append("mapToObject", Common.END));
return object;
}
/**
*
* 〈得到map集合的字段〉
* @param [object]     [Object的对象]
* @return  [Map]
* @Author Erin */
public Map getFields(Object object) {
logger.debug(StringUtil.append("getFields", Common.BEGIN));
Map map = new HashMap();
Method[] methods = object.getClass().getMethods();
for (Method method : methods) {
String name = method.getName();
if (name.startsWith("set")) {
String field = name.substring(3, 4).toLowerCase()
+ name.substring(4);
map.put(field, null);
}
}
logger.debug(StringUtil.append("getFields", Common.END));
return map;

}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics