从request中获取值填充bean对象

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

/**
* <strong>封装数据类</strong>
* <p>
* 从request中获取值填充bean对象。
* 相应功能struts中已经实现,建议使用struts进行页面到Form的处理。
* </p>
*
*/
public class BeanUtil {
private static final Logger log = Logger.getLogger(BeanUtil.class);
/**
* 从request中获取值填充bean对象
*
* @param objClass bean的类
* @param request 请求对象
* @return object对象
*/
public static Object fillBean(Class objClass, HttpServletRequest request) {
Object objInstance = null;
try {
objInstance = objClass.newInstance();
} catch (InstantiationException e1) {
log.error(e1.getMessage(), e1);
} catch (IllegalAccessException e1) {
log.error(e1.getMessage(), e1);
}

Field field;
String fieldName;
String fieldValue = "";
int len;
len = objClass.getDeclaredFields().length;
for (int i = 0; i < len; i++) {
field = objClass.getDeclaredFields()[i];
fieldName = field.getName();

try {
fieldValue = request.getParameter(fieldName);
} catch (Exception e1) {
log.error(e1.getMessage(), e1);
}

if (fieldValue != null) {
try {
setFieldValue(field, objInstance, fieldValue);
} catch (IllegalAccessException e) {
log.error(e.getMessage(), e);
}
}
}
objClass = objClass.getSuperclass();
return objInstance;
}

/**
* 将数据赋值给指定对象的相应属性
*
* @param field 字段
* @param objInstance 指定对象
* @param value 数据
* @throws IllegalAccessException
*/
private static void setFieldValue(Field field, Object objInstance, String value) throws IllegalAccessException {
String fieldType = field.getType().getName();// 取字段的数据类型
field.setAccessible(true);
try {
if (fieldType.equals("java.lang.String")) {
field.set(objInstance, value);
} else if (fieldType.equals("java.lang.Integer") || fieldType.equals("int")) {
field.set(objInstance, Integer.valueOf(value));
} else if (fieldType.equals("java.lang.Long") || fieldType.equals("long")) {
field.set(objInstance, Long.valueOf(value));
} else if (fieldType.equals("java.lang.Float") || fieldType.equals("float")) {
field.set(objInstance, Float.valueOf(value));
} else if (fieldType.equals("java.lang.Double") || fieldType.equals("double")) {
field.set(objInstance, Double.valueOf(value));
} else if (fieldType.equals("java.math.BigDecimal")) {
field.set(objInstance, new BigDecimal(value));
} else if (fieldType.equals("java.util.Date")) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
formatter.setLenient(false);
field.set(objInstance, formatter.parse(value));
} else if (fieldType.equals("java.sql.Date")) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
formatter.setLenient(false);
Date date = formatter.parse(value);
field.set(objInstance, new java.sql.Date(date.getTime()));
} else if (fieldType.equals("java.lang.Boolean") || fieldType.equals("boolean")) {
field.set(objInstance, Boolean.valueOf(value));
} else if (fieldType.equals("java.lang.Byte") || fieldType.equals("byte")) {
field.set(objInstance, Byte.valueOf(value));
} else if (fieldType.equals("java.lang.Short") || fieldType.equals("short")) {
field.set(objInstance, Short.valueOf(value));
}
} catch (NumberFormatException ex) {
// field.set(objInstance, null); 当使用简单数据类型会抛出异常
log.error(ex.getMessage(), ex);
} catch (ParseException e) {
log.error(e.getMessage(), e);
field.set(objInstance, null);
} catch (Exception e) {
log.error(e.getMessage(), e);
field.set(objInstance, null);
}
}
}




import java.beans.PropertyDescriptor;
import java.text.SimpleDateFormat;
import java.util.Collection;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;

/**
* <strong>复制处理类</strong>
*/
public class CopyUtil {
private static final Logger log = Logger.getLogger(CopyUtil.class.getName());

/**
* 复制orig属性值赋值给dest
* <p>
* 满足如下条件时进行复制处理:
* (1) dest与orig的属性名称相同;
* (2) dest与orig的属性类型相同;
* (3) dest的属性类型不为Class;
* (4) orig属性类型不是Collection或者不是Collection的超类或接口;
* </p>
*
* @param dest 目标对象
* @param orig 源对象
* @return 如果dest或者orig为null则返回null/如果发生异常返回null/否则返回复制填充后的dest
*/
public static Object copyProperties(Object dest, Object orig) {
if (dest == null || orig == null) {
return dest;
}
PropertyDescriptor[] destDesc = PropertyUtils.getPropertyDescriptors(dest);
try {
for (int i = 0; i < destDesc.length; i++) {
Class destType = destDesc[i].getPropertyType();
Class origType = PropertyUtils.getPropertyType(orig, destDesc[i].getName());
if (destType != null && destType.equals(origType) && !destType.equals(Class.class)) {
if (!Collection.class.isAssignableFrom(origType)) {
try {
Object value = PropertyUtils.getProperty(orig, destDesc[i].getName());
PropertyUtils.setProperty(dest, destDesc[i].getName(), value);
} catch (Exception ex) {
log.error("copyProperties(Object dest, Object orig)循环中异常。", ex);
}
}
}
}

return dest;
} catch (Exception ex) {
log.error("copyProperties(Object dest, Object orig)异常。", ex);
return null;
}
}

/**
* 复制orig属性值赋值给dest
* <p>
* 除了将String的值转换为java.util.Date的处理外,其他处理方式同copyProperties(Object dest, Object orig)。
* 最常用在将页面的查询条件(Form)转换为DTO进行数据库查询的处理。
* </p>
*
* @param dest 目标对象
* @param orig 源对象
* @return 如果dest或者orig为null则返回null/如果发生异常返回null/否则返回复制填充后的dest
*/
public static Object copyPropertiesSpecialOfStringToDate(Object dest, Object orig) {
if (dest == null || orig == null) {
return dest;
}
PropertyDescriptor[] destDesc = PropertyUtils.getPropertyDescriptors(dest);
try {
for (int i = 0; i < destDesc.length; i++) {
Class destType = destDesc[i].getPropertyType();
Class origType = PropertyUtils.getPropertyType(orig, destDesc[i].getName());
if (destType != null && origType != null && !destType.equals(Class.class)) {
if (origType.equals(destType)) {
if (!Collection.class.isAssignableFrom(origType)) {
try {
Object valueOrig = PropertyUtils.getProperty(orig, destDesc[i].getName());
PropertyUtils.setProperty(dest, destDesc[i].getName(), valueOrig);
} catch (Exception ex) {
log.error("copyPropertiesSpecialOfStringToDate循环中异常-1。", ex);
PropertyUtils.setProperty(dest, destDesc[i].getName(), null);
}
}
} else if (destType.equals(java.util.Date.class) && origType.equals(String.class)) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
String valueOrig_string = (String) PropertyUtils.getProperty(orig, destDesc[i].getName());
if (valueOrig_string != null && valueOrig_string.trim().length() > 0) {
PropertyUtils.setProperty(dest, destDesc[i].getName(), format.parse(valueOrig_string));
} else {
PropertyUtils.setProperty(dest, destDesc[i].getName(), null);
}
} catch (Exception e) {
log.error("copyPropertiesSpecialOfStringToDate循环中异常-2。", e);
PropertyUtils.setProperty(dest, destDesc[i].getName(), null);
}
}
}
}
return dest;
} catch (Exception e) {
log.error("copyPropertiesSpecialOfStringToDate异常。", e);
return null;
}
}

/**
* 复制orig中指定的属性值赋值给dest
* <p>
* 除了指定属性名称数组外,其他处理方式同copyProperties(Object dest, Object orig)。
* </p>
*
* @param dest
* @param orig
* @param ignores
* @return
*/
public static Object copyProperties(Object dest, Object orig, String[] ignores) {
if (dest == null || orig == null) {
return dest;
}

PropertyDescriptor[] destDesc = PropertyUtils.getPropertyDescriptors(dest);
try {
for (int i = 0; i < destDesc.length; i++) {

if (contains(ignores, destDesc[i].getName())) {
continue;
}

Class destType = destDesc[i].getPropertyType();
Class origType = PropertyUtils.getPropertyType(orig, destDesc[i].getName());

if (destType != null && destType.equals(origType) && !destType.equals(Class.class)) {
if (!Collection.class.isAssignableFrom(origType)) {
Object value = PropertyUtils.getProperty(orig, destDesc[i].getName());
PropertyUtils.setProperty(dest, destDesc[i].getName(), value);
}
}
}

return dest;
} catch (Exception ex) {
return null;
}
}

/**
* 属性名称数组中是否存在指定的属性
*
* @param ignores 属性名称数组
* @param name 指定的属性名称
* @return 如果存在则返回true/否则返回false
*/
static boolean contains(String[] ignores, String name) {
boolean ignored = false;
for (int j = 0; ignores != null && j < ignores.length; j++) {
if (ignores[j].equals(name)) {
ignored = true;
break;
}
}
return ignored;
}
}



CopyUtil.copyProperties(userManagerDTO, initUserForm);

TaskDTO task = (TaskDTO) BeanUtil.fillBean(TaskDTO.class, request);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值