public static void populate(Object bean,HttpRequest request, Map<?, ?> m)
{
ListOrderedMap orderedMap = new ListOrderedMap();
Map<?, ?> map=request.getParameterMap();
orderedMap.putAll(map);
PropertyUtils propertyUtils = new PropertyUtils();
@SuppressWarnings("unchecked")
List<String> list=orderedMap.asList();
for (String string : list)
{
String[] name = StringUtils.split(string, ".");
if (name.length==1)
{
String objectString= (String) m.get(string);
if (propertyUtils.isReadable(bean, string)&&propertyUtils.isWriteable(bean, string)&&objectString!=null)
{
try {
if (StringUtils.containsIgnoreCase(objectString, "java.lang.String"))
{
//设置String
propertyUtils.setProperty(bean, string, request.getParameter(string));
}else if (StringUtils.containsIgnoreCase(objectString, "java.lang.Integer")
||StringUtils.containsIgnoreCase(objectString, "java.lang.Short")
||StringUtils.containsIgnoreCase(objectString, "java.lang.Long")
||StringUtils.containsIgnoreCase(objectString, "java.lang.Float")
||StringUtils.containsIgnoreCase(objectString, "java.lang.Double")
||StringUtils.containsIgnoreCase(objectString, "java.lang.Byte")
||StringUtils.containsIgnoreCase(objectString, "java.math.BigInteger")
||StringUtils.containsIgnoreCase(objectString, "java.math.BigDecimal"))
{
if (NumberUtils.isNumber(request.getParameter(string))) {
//设置Number
propertyUtils.setProperty(bean, string, NumberUtils.createNumber(request.getParameter(string)));
}
}else if (StringUtils.containsIgnoreCase(objectString, "java.util.Date"))
{
String s = request.getParameter(string);
if (StringUtils.isNotEmpty(s))
{
//设置时间
String[] parsePatterns ={"yyyy/MM/dd","yyyy/MM/dd HH:mm:ss",
"yyyy.MM.dd","yyyy.MM.dd HH:mm:ss",
"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss",
"HH:mm:ss"};
Date parsedDate = null;
try {
parsedDate= DateUtils.parseDate(s,
parsePatterns);
} catch (ParseException e ) {
log.error("{}不是正确的时间格式",s);
}
propertyUtils.setProperty(bean, string, parsedDate);
}
}
} catch (IllegalAccessException e)
{log.error("给类{}赋值错误!",bean);}
catch (InvocationTargetException e)
{log.error("给类{}赋值错误!",bean);}
catch ( NoSuchMethodException e)
{
log.error("给类{}赋值错误!",bean);
}
}
}else if (name.length==2)
{
String objectString= (String) m.get(name[0]);
if (StringUtils.isNotEmpty(objectString))
{
try {
Object object=Class.forName(objectString).newInstance();
if (propertyUtils.isReadable(bean, name[0])&&propertyUtils.isWriteable(bean, name[0]))
{
if (PropertyUtils.getProperty(bean, name[0]) == null)
{
//如果没有初始化,求初始
PropertyUtils.setProperty(bean, name[0], object);
}
ClassParsing cp = new ClassParsing();
ClassLoader classloader = object.getClass().getClassLoader();
if (classloader != null) {
URL url = classloader.getResource(StringUtils.replace(object.getClass().getName(), ".", "/") + ".class");
File file = FileUtils.toFile(url);
if (file != null)
{
ClassReader cr = new ClassReader(FileUtils.openInputStream(file));
cr.accept(cp, 0);
}
}
Map<?, ?> ma=cp.getMap();
String ob= (String) ma.get(name[1]);
if (propertyUtils.isReadable(bean, string)&&propertyUtils.isWriteable(bean, string)&&ob!=null)
{
if (StringUtils.containsIgnoreCase(ob, "java.lang.String"))
{
propertyUtils.setProperty(bean, string, request.getParameter(string));
}else if (StringUtils.containsIgnoreCase(ob, "java.lang.Integer")
||StringUtils.containsIgnoreCase(ob, "java.lang.Short")
||StringUtils.containsIgnoreCase(ob, "java.lang.Long")
||StringUtils.containsIgnoreCase(ob, "java.lang.Float")
||StringUtils.containsIgnoreCase(ob, "java.lang.Double")
||StringUtils.containsIgnoreCase(ob, "java.lang.Byte")
||StringUtils.containsIgnoreCase(ob, "java.math.BigInteger")
||StringUtils.containsIgnoreCase(ob, "java.math.BigDecimal"))
{
if (NumberUtils.isNumber(request.getParameter(string))) {
propertyUtils.setProperty(bean, string, NumberUtils.createNumber(request.getParameter(string)));
}
}else if (StringUtils.containsIgnoreCase(ob, "java.util.Date"))
{
String s = request.getParameter(string);
if (StringUtils.isNotEmpty(s))
{
String[] parsePatterns ={"yyyy/MM/dd","yyyy/MM/dd HH:mm:ss",
"yyyy.MM.dd","yyyy.MM.dd HH:mm:ss",
"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss",
"HH:mm:ss"};
Date parsedDate = null;
try {
parsedDate= DateUtils.parseDate(s,
parsePatterns);
} catch (ParseException e ) {
log.error("{}不是正确的时间格式",s);
}
propertyUtils.setProperty(bean, string, parsedDate);
}
}
}