package com.bjpowernode.common.util;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.beanutils.PropertyUtils;
public class MyBeanUtils
extends org.apache.commons.beanutils.BeanUtils {
private static void convert(Object dest, Object orig) throws
IllegalAccessException, InvocationTargetException {
// Validate existence of the specified beans
if (dest == null) {
throw new IllegalArgumentException
("No destination bean specified");
}
if (orig == null) {
throw new IllegalArgumentException("No origin bean specified");
}
// Copy the properties, converting as necessary
if (orig instanceof DynaBean) {
DynaProperty origDescriptors[] =
( (DynaBean) orig).getDynaClass().getDynaProperties();
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
if (PropertyUtils.isWriteable(dest, name)) {
Object value = ( (DynaBean) orig).get(name);
try {
copyProperty(dest, name, value);
}
catch (Exception e) {
; // Should not happen
}
}
}
}
else if (orig instanceof Map) {
Iterator names = ( (Map) orig).keySet().iterator();
while (names.hasNext()) {
String name = (String) names.next();
if (PropertyUtils.isWriteable(dest, name)) {
Object value = ( (Map) orig).get(name);
try {
copyProperty(dest, name, value);
}
catch (Exception e) {
; // Should not happen
}
}
}
}
else
/* if (orig is a standard JavaBean) */
{
PropertyDescriptor origDescriptors[] =
PropertyUtils.getPropertyDescriptors(orig);
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
// String type = origDescriptors[i].getPropertyType().toString();
if ("class".equals(name)) {
continue; // No point in trying to set an object's class
}
if (PropertyUtils.isReadable(orig, name) &&
PropertyUtils.isWriteable(dest, name)) {
try {
Object value = PropertyUtils.getSimpleProperty(orig, name);
copyProperty(dest, name, value);
}
catch (java.lang.IllegalArgumentException ie) {
; // Should not happen
}
catch (Exception e) {
; // Should not happen
}
}
}
}
}
/**
* 对象拷贝
* 数据对象空值不拷贝到目标对象
*
* @param dataObject
* @param toObject
* @throws NoSuchMethodException
* copy
*/
public static void copyBeanNotNull2Bean(Object databean,Object tobean)throws Exception
{
PropertyDescriptor origDescriptors[] =
PropertyUtils.getPropertyDescriptors(databean);
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
// String type = origDescriptors[i].getPropertyType().toString();
if ("class".equals(name)) {
continue; // No point in trying to set an object's class
}
if (PropertyUtils.isReadable(databean, name) &&
PropertyUtils.isWriteable(tobean, name)) {
try {
Object value = PropertyUtils.getSimpleProperty(databean, name);
if(value!=null){
copyProperty(tobean, name, value);
}
}
catch (java.lang.IllegalArgumentException ie) {
; // Should not happen
}
catch (Exception e) {
; // Should not happen
}
}
}
}
/**
* 把orig和dest相同属性的value复制到dest中
* @param dest
* @param orig
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static void copyBean2Bean(Object dest, Object orig) throws Exception {
convert(dest, orig);
}
public static void copyBean2Map(Map map, Object bean){
PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(bean);
for (int i =0;i<pds.length;i++)
{
PropertyDescriptor pd = pds[i];
String propname = pd.getName();
try {
Object propvalue = PropertyUtils.getSimpleProperty(bean,propname);
map.put(propname, propvalue);
} catch (IllegalAccessException e) {
//e.printStackTrace();
} catch (InvocationTargetException e) {
//e.printStackTrace();
} catch (NoSuchMethodException e) {
//e.printStackTrace();
}
}
}
/**
* 将Map内的key与Bean中属性相同的内容复制到BEAN中
* @param bean Object
* @param properties Map
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static void copyMap2Bean(Object bean, Map properties) throws
IllegalAccessException, InvocationTargetException {
// Do nothing unless both arguments have been specified
if ( (bean == null) || (properties == null)) {
return;
}
// Loop through the property name/value pairs to be set
Iterator names = properties.keySet().iterator();
while (names.hasNext()) {
String name = (String) names.next();
// Identify the property name and value(s) to be assigned
if (name == null) {
continue;
}
Object value = properties.get(name);
try {
Class clazz = PropertyUtils.getPropertyType(bean, name);
if (null == clazz) {
continue;
}
String className = clazz.getName();
if (className.equalsIgnoreCase("java.sql.Timestamp")) {
if (value == null || value.equals("")) {
continue;
}
}
setProperty(bean, name, value);
}
catch (NoSuchMethodException e) {
continue;
}
}
}
/**
* 自动转Map key值大写
* 将Map内的key与Bean中属性相同的内容复制到BEAN中
* @param bean Object
* @param properties Map
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static void copyMap2Bean_Nobig(Object bean, Map properties) throws
IllegalAccessException, InvocationTargetException {
// Do nothing unless both arguments have been specified
if ( (bean == null) || (properties == null)) {
return;
}
// Loop through the property name/value pairs to be set
Iterator names = properties.keySet().iterator();
while (names.hasNext()) {
String name = (String) names.next();
// Identify the property name and value(s) to be assigned
if (name == null) {
continue;
}
Object value = properties.get(name);
// 命名应该大小写应该敏感(否则取不到对象的属性)
//name = name.toLowerCase();
try {
if (value == null) { // 不光Date类型,好多类型在null时会出错
cont
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
基于java web的宿舍管理系统源码.zip (602个子文件)
LoginController.class 8KB
RoleController.class 8KB
StudentController.class 8KB
UserController.class 8KB
DormController.class 6KB
ResourceController.class 6KB
ScoreController.class 6KB
MyBeanUtils.class 6KB
VisitorController.class 6KB
DamageController.class 6KB
ClassController.class 5KB
AuthInterceptor.class 4KB
VisitorServiceImpl.class 4KB
BaseDaoImpl.class 4KB
CaptchaUtil.class 4KB
SystemDaoImpl.class 4KB
SystemServiceImpl.class 4KB
UserEntity.class 4KB
ScoreServiceImpl.class 4KB
BrowserUtils.class 4KB
StudentServiceImpl.class 4KB
DormServiceImpl.class 3KB
DamageServiceImpl.class 3KB
ClassServiceImpl.class 3KB
ResourceUtil.class 3KB
ResourceEntity.class 3KB
BaseServiceImpl.class 3KB
TreeGrid.class 3KB
StudentEntity.class 3KB
CaptchaServlet.class 3KB
TreeNode.class 2KB
TreeGridModel.class 2KB
VisitorEntity.class 2KB
DepartEntity.class 2KB
DamageEntity.class 2KB
RoleEntity.class 2KB
AjaxJson.class 2KB
DateConvertEditor.class 2KB
Pagination.class 2KB
Client.class 2KB
DormEntity.class 2KB
ClientManager.class 2KB
ScoreEntity.class 2KB
BrowserType.class 2KB
Data.class 1KB
BaseEntity.class 1KB
ClassEntity.class 1KB
JsonUtil.class 991B
ContextHolderUtils.class 983B
BaseController.class 931B
SystemService.class 925B
SystemDao.class 744B
BaseDao.class 729B
BaseService.class 690B
ScoreService.class 655B
StudentService.class 591B
VisitorService.class 591B
DamageService.class 587B
DormService.class 579B
ClassService.class 547B
SystemConstant.class 410B
.classpath 834B
org.eclipse.wst.common.component 500B
org.eclipse.wst.jsdt.ui.superType.container 49B
easyui.css 51KB
easyui.css 51KB
easyui.css 51KB
easyui.css 50KB
easyui.css 47KB
tabs.css 8KB
tabs.css 8KB
tabs.css 8KB
tabs.css 8KB
tabs.css 6KB
datagrid.css 5KB
datagrid.css 5KB
datagrid.css 5KB
datagrid.css 5KB
datagrid.css 5KB
linkbutton.css 4KB
linkbutton.css 4KB
linkbutton.css 4KB
linkbutton.css 4KB
linkbutton.css 4KB
style.css 4KB
color.css 4KB
calendar.css 4KB
calendar.css 4KB
calendar.css 4KB
calendar.css 4KB
calendar.css 4KB
tree.css 3KB
tree.css 3KB
tree.css 3KB
tree.css 3KB
tree.css 3KB
panel.css 3KB
panel.css 3KB
panel.css 3KB
panel.css 3KB
共 602 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
老了敲不动了
- 粉丝: 83
- 资源: 4289
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功