📄 valueobjecthelper.java
字号:
/*
* Created on Jul 19, 2006 4:11:22 PM
*
* By SinoBest
* Copyright hnisi.com.cn, 2005-2006, All rights reserved.
*/
package cn.com.juneng.system.common.util;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Constructor;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.PropertyUtils;
import cn.com.juneng.system.common.COMMON;
/**
* @author wanghaifeng
* @author yehailong
*
*/
public class ValueObjectHelper {
/**
* 自动接受HttpServletRequest参数,填充值对象
*
* @param target
* 目标对象
* @param req
* HttpServletRequest
*/
public static void fillObjectBean(Object target, HttpServletRequest req) {
PropertyDescriptor descriptors[] = PropertyUtils
.getPropertyDescriptors(target.getClass());
Enumeration paramNames = req.getParameterNames();
Map namesMap = new HashMap();
while (paramNames.hasMoreElements()) {
String name = (String) paramNames.nextElement();
if (name != null) {
namesMap.put(name.toLowerCase(), name);
}
}
for (int i = 0; i < descriptors.length; i++) {
String property = descriptors[i].getName();
if (!namesMap.containsKey(property.toLowerCase()))
continue;
Class propClass = descriptors[i].getPropertyType();
try {
String paramName = (String) namesMap
.get(property.toLowerCase());
String valueStr = req.getParameter(paramName);
if (valueStr == null || valueStr.trim().length() == 0) {
continue;
}
Object value = null;
if (!propClass.isPrimitive()) {
// 不是基本类型
if (propClass == Timestamp.class) {
value = formatToTimestamp(valueStr);
} else if (propClass == java.sql.Date.class) {
value = formatToSQLDATE(valueStr);
} else {
// 通过构造函数转换string
Constructor constructor = propClass
.getConstructor(new Class[] { Class
.forName("java.lang.String") });
value = constructor
.newInstance(new String[] { valueStr });
}
} else {
// 基本类型
if (Integer.TYPE == propClass) {
value = new Integer(valueStr);
} else if (Double.TYPE == propClass) {
value = new Double(valueStr);
} else if (Boolean.TYPE == propClass) {
value = new Boolean(valueStr);
} else if (Long.TYPE == propClass) {
value = new Long(valueStr);
} else if (Float.TYPE == propClass) {
value = new Float(valueStr);
} else if (Byte.TYPE == propClass) {
value = new Byte(valueStr);
} else if (Short.TYPE == propClass) {
value = new Short(valueStr);
}
}
PropertyUtils.setProperty(target, property, value);
//PropertyUtils.setNestedProperty(target, property, value);
} catch (Exception e) {
e.printStackTrace();
// ignore
}
}
}
public static Timestamp formatToTimestamp(String sTimestamp) {
if (sTimestamp == null || sTimestamp.equals(""))
return null;
String sTemp = sTimestamp.trim();
if (sTemp.length() < 12)
sTemp = sTemp + " 00:00:00";
Timestamp time = null;
try {
time = new Timestamp(new SimpleDateFormat(COMMON.TIME_FORMAT)
.parse(sTemp).getTime());
} catch (Exception e) {
e.printStackTrace();
}
return time;
}
public static Date formatToSQLDATE(String sDate) {
if (sDate == null || sDate.equals("")) {
return null;
} else {
Date date = new Date(formatToTimestamp(sDate).getTime());
return date;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -