如何实现jsp javabeann的属性拷贝

实现JavaBean的属性设置和拷贝_小组_ThinkSAAS
实现JavaBean的属性设置和拷贝
实现JavaBean的属性设置和拷贝
a中有对象属性b
b中有基本对象c
则通过参数 a和a.b.c获得c的值
或者b为数组
通过参数 a和a.b[1].c 获得c的值
public static Object getProperty(Object bean, String name)为入口
程序代码如下:
import java.beans.BeanI
import java.beans.IndexedPropertyD
import java.beans.IntrospectionE
import java.beans.I
import java.beans.PropertyD
import java.lang.reflect.A
import java.lang.reflect.InvocationTargetE
import java.lang.reflect.M
import java.lang.reflect.M
import java.util.HashM
import java.util.M
public class BeanUtils {
private static HashMap descriptorsC
descriptorsCache = new HashMap();
public static Map describe(Object bean) throws IllegalAccessException,
InvocationTargetException,
NoSuchMethodException
if (bean == null)
throw new IllegalArgumentException("No bean specified");
PropertyDescriptor descriptors[] = getPropertyDescriptors(bean);
Map description = new HashMap(descriptors.length);
for (int i = 0; i & descriptors. i++) {
String name = descriptors[i].getName();
if (descriptors[i].getReadMethod() != null)
description.put(name, getProperty(bean, name));
public static Object getIndexedProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
int delim = name.indexOf('[');
int delim2 = name.indexOf(']');
if (delim & 0 || delim2 &= delim)
throw new IllegalArgumentException("Invalid indexed property '"
+ name +"'");
int index = -1;
String subscript = name.substring(delim + 1, delim2);
index = Integer.parseInt(subscript);
} catch (NumberFormatException numberformatexception) {
throw new IllegalArgumentException("Invalid indexed property '"
+ name +"'");
name = name.substring(0, delim);
return getIndexedProperty(bean, name, index);
public static Object getIndexedProperty(Object bean, String name, int index)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);
if (descriptor == null)
throw new NoSuchMethodException("Unknown property '"+ name +"'");
Method readM
if (descriptor instanceof IndexedPropertyDescriptor) {
readMethod = ((IndexedPropertyDescriptor) descriptor)
.getIndexedReadMethod();
if (readMethod != null) {
Object subscript[] = new Object[1];
subscript[0] = new Integer(index);
return readMethod.invoke(bean, subscript);
readMethod = getReadMethod(descriptor);
if (readMethod == null)
throw new NoSuchMethodException("Property '"+ name
+"' has no getter method");
Object value = readMethod.invoke(bean, new Object[0]);
if (!value.getClass().isArray())
throw new IllegalArgumentException("Property '"+ name
+"' is not indexed");
return Array.get(value, index);
public static Object getNestedProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
int delim = name.indexOf('.');
if (delim & 0)
String next = name.substring(0, delim);
if (next.indexOf('[') &= 0)
bean = getIndexedProperty(bean, next);
bean = getSimpleProperty(bean, next);
if (bean == null)
throw new IllegalArgumentException("Null property value for '"
+ name.substring(0, delim) +"'");
name = name.substring(delim + 1);
} while (true);
if (name.indexOf('[') &= 0)
return getIndexedProperty(bean, name);
return getSimpleProperty(bean, name);
public static Object getProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
//System.out.println("----------"+ bean.getClass().getName() +"name="+ name);
return getNestedProperty(bean, name);
public static PropertyDescriptor getPropertyDescriptor(Object bean,
String name) throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
int period = name.indexOf('.');
if (period & 0)
String next = name.substring(0, period);
if (next.indexOf('[') &= 0)
bean = getIndexedProperty(bean, next);
bean = getSimpleProperty(bean, next);
if (bean == null)
throw new IllegalArgumentException("Null property value for '"
+ name.substring(0, period) +"'");
name = name.substring(period + 1);
} while (true);
System.out.println("----start 13--");
int left = name.indexOf('[');
if (left &= 0)
name = name.substring(0, left);
if (bean == null || name == null)
PropertyDescriptor descriptors[] = getPropertyDescriptors(bean);
if (descriptors == null)
for (int i = 0; i & descriptors. i++)
if (name.equals(descriptors[i].getName()))
return descriptors[i];
* @param bean
返回该对象的属性描述集合对象
public static PropertyDescriptor[] getPropertyDescriptors(Object bean) {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
String beanClassName = bean.getClass().getName();
PropertyDescriptor descriptors[] =
descriptors = (PropertyDescriptor[]) descriptorsCache
.get(beanClassName);
if (descriptors != null)
BeanInfo beanInfo =
beanInfo = Introspector.getBeanInfo(bean.getClass());
} catch (IntrospectionException introspectionexception) {
return new PropertyDescriptor[0];
descriptors = beanInfo.getPropertyDescriptors();
if (descriptors == null)
descriptors = new PropertyDescriptor[0];
descriptorsCache.put(beanClassName, descriptors);
public static Class getPropertyEditorClass(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);
if (descriptor != null)
return descriptor.getPropertyEditorClass();
public static Class getPropertyType(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);
if (descriptor == null)
if (descriptor instanceof IndexedPropertyDescriptor)
return ((IndexedPropertyDescriptor) descriptor)
.getIndexedPropertyType();
return descriptor.getPropertyType();
public static Method getReadMethod(PropertyDescriptor descriptor) {
return getAccessibleMethod(descriptor.getReadMethod());
public static Object getSimpleProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
if (name.indexOf('.') &= 0)
throw new IllegalArgumentException(
"Nested property names are not allowed");
if (name.indexOf('[') &= 0)
throw new IllegalArgumentException(
"Indexed property names are not allowed");
PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);
if (descriptor == null)
throw new NoSuchMethodException("Unknown property '"+ name +"'");
Method readMethod = getReadMethod(descriptor);
if (readMethod == null) {
throw new NoSuchMethodException("Property '"+ name
+"' has no getter method");
Object value = readMethod.invoke(bean, new Object[0]);
public static Method getWriteMethod(PropertyDescriptor descriptor) {
return getAccessibleMethod(descriptor.getWriteMethod());
public static void setIndexedProperty(Object bean, String name, Object value)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
int delim = name.indexOf('[');
int delim2 = name.indexOf(']');
if (delim & 0 || delim2 &= delim)
throw new IllegalArgumentException("Invalid indexed property '"
+ name +"'");
int index = -1;
String subscript = name.substring(delim + 1, delim2);
index = Integer.parseInt(subscript);
} catch (NumberFormatException numberformatexception) {
throw new IllegalArgumentException("Invalid indexed property '"
+ name +"'");
name = name.substring(0, delim);
setIndexedProperty(bean, name, index, value);
public static void setIndexedProperty(Object bean, String name, int index,
Object value) throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);
if (descriptor == null)
throw new NoSuchMethodException("Unknown property '"+ name +"'");
if (descriptor instanceof IndexedPropertyDescriptor) {
Method writeMethod = ((IndexedPropertyDescriptor) descriptor)
.getIndexedWriteMethod();
if (writeMethod != null) {
Object subscript[] = new Object[2];
subscript[0] = new Integer(index);
subscript[1] =
writeMethod.invoke(bean, subscript);
Method readMethod = descriptor.getReadMethod();
if (readMethod == null)
throw new NoSuchMethodException("Property '"+ name
+"' has no getter method");
Object array = readMethod.invoke(bean, new Object[0]);
if (!array.getClass().isArray()) {
throw new IllegalArgumentException("Property '"+ name
+"' is not indexed");
Array.set(array, index, value);
public static void setNestedProperty(Object bean, String name, Object value)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
int delim = name.indexOf('.');
if (delim & 0)
String next = name.substring(0, delim);
if (next.indexOf('[') &= 0)
bean = getIndexedProperty(bean, next);
bean = getSimpleProperty(bean, next);
if (bean == null)
throw new IllegalArgumentException("Null property value for '"
+ name.substring(0, delim) +"'");
name = name.substring(delim + 1);
} while (true);
if (name.indexOf('[') &= 0)
setIndexedProperty(bean, name, value);
setSimpleProperty(bean, name, value);
public static void setProperty(Object bean, String name, Object value)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
setNestedProperty(bean, name, value);
public static void setSimpleProperty(Object bean, String name, Object value)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
if (bean == null)
throw new IllegalArgumentException("No bean specified");
if (name == null)
throw new IllegalArgumentException("No name specified");
if (name.indexOf('.') &= 0)
throw new IllegalArgumentException(
"Nested property names are not allowed");
if (name.indexOf('[') &= 0)
throw new IllegalArgumentException(
"Indexed property names are not allowed");
PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);
if (descriptor == null)
throw new NoSuchMethodException("Unknown property '"+ name +"'");
Method writeMethod = getWriteMethod(descriptor);
if (writeMethod == null) {
throw new NoSuchMethodException("Property '"+ name
+"' has no setter method");
Object values[] = new Object[1];
values[0] =
writeMethod.invoke(bean, values);
private static Method getAccessibleMethod(Method method) {
if (method == null)
if (!Modifier.isPublic(method.getModifiers()))
Class clazz = method.getDeclaringClass();
if (Modifier.isPublic(clazz.getModifiers())) {
String methodName = method.getName();
Class parameterTypes[] = method.getParameterTypes();
method = getAccessibleMethodFromInterfaceNest(clazz, method
.getName(), method.getParameterTypes());
private static Method getAccessibleMethodFromInterfaceNest(Class clazz,
String methodName, Class parameterTypes[]) {
Method method =
Class interfaces[] = clazz.getInterfaces();
for (int i = 0; i & interfaces. i++) {
if (!Modifier.isPublic(interfaces[i].getModifiers()))
method = interfaces[i].getDeclaredMethod(methodName,
parameterTypes);
} catch (NoSuchMethodException nosuchmethodexception) {
if (method != null)
method = getAccessibleMethodFromInterfaceNest(interfaces[i],
methodName, parameterTypes);
if (method != null)
public static final char INDEXED_DELIM = 91;
public static final char INDEXED_DELIM2 = 93;
public static final char NESTED_DELIM = 46;
private static int debug = 0;
用户评论(0)
开发技术学习小组列表
PHP开发框架
缓存Memcache
服务器环境
ThinkSAAS商业授权:
ThinkSAAS为用户提供有偿个性定制开发服务
ThinkSAAS将为商业授权用户提供二次开发指导和技术支持
手机客户端
ThinkSAAS接收任何功能的Iphone(IOS)和Android手机的客户端定制开发服务
让ThinkSAAS更好,把建议拿来。
iphone扫码下载客户端1017人阅读
在struts的实践过程中,经常遇到两个javabean交换数据的情况,如ActionForm与数据库中的表相关的bean交换数据。通常情况下要写很多get和set语句,一个个属性依次拷贝。这样的话,如此重复繁重的工作让程序员感觉不到编程的快乐。于是在网上查相关资料知,在apache.org有一个project:common-beanutil,提供的一个BeanUtil类,这个类有一个静态方法BeanUtil.copyProperties()实现了该功能。后来我在与java相关的书上知道的java的反射机制(reflect),尝试着并实现了两个javabean的属性拷贝功能。
用到反射机制&
此方法将调用obj1的getter方法,将得到的值作为相应的参数传给obj2的setter方法。
注意,obj1的getter方法和obj2方法必须是public类型。
import java.lang.reflect.M
public class BeanUtil {
@parameter Object obj1,Object obj2
@return Object
用到反射机制
此方法将调用obj1的getter方法,将得到的值作为相应的参数传给obj2的setter方法
注意,obj1的getter方法和obj2方法必须是public类型
public static Object CopyBeanToBean(Object obj1,Object obj2) throws Exception{
Method[] method1=obj1.getClass().getMethods();
Method[] method2=obj2.getClass().getMethods();
String methodName1;
String methodFix1;
String methodName2;
String methodFix2;
for(int i=0;i&method1.i++){
methodName1=method1[i].getName();
methodFix1=methodName1.substring(3,methodName1.length());
if(methodName1.startsWith(&get&)){
for(int j=0;j&method2.j++){
methodName2=method2[j].getName();
methodFix2=methodName2.substring(3,methodName2.length());
if(methodName2.startsWith(&set&)){
if(methodFix2.equals(methodFix1)){
Object[] objs1=new Object[0];
Object[] objs2=new Object[1];
objs2[0]=method1[i].invoke(obj1,objs1);//激活obj1的相应的get的方法,objs1数组存放调用该方法的参数,此例中没有参数,该数组的长度为0
method2[j].invoke(obj2,objs2);//激活obj2的相应的set的方法,objs2数组存放调用该方法的参数
return obj2;
再建一个javabean,并测试
import java.lang.reflect.*;
public class User {
public void setName(String name) {
this.name =
public String getName() {
return this.
public void setId(String id) {
public String getId() {
return this.
public static void main(String[] args) throws Exception {
User u1 = new User();
u1.setName(&zxb&);
u1.setId(&3286&);
User u2 = new User();
u2 = (User) BeanUtil2.CopyBeanToBean(u1, u2);
System.out.println(u2.getName());
System.out.println(u2.getId());
编译后并执行输出结果:
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1846384次
积分:20851
积分:20851
排名:第152名
原创:133篇
转载:887篇
评论:405条
(1)(1)(3)(5)(1)(2)(1)(2)(3)(1)(7)(2)(3)(4)(6)(7)(13)(17)(13)(4)(7)(3)(14)(35)(15)(17)(3)(1)(3)(8)(8)(11)(4)(8)(22)(28)(18)(9)(8)(5)(13)(22)(40)(21)(5)(19)(22)(28)(4)(8)(14)(7)(16)(12)(32)(21)(4)(10)(67)(57)(8)(10)(4)(6)(9)(16)(9)(4)(17)(34)(23)(16)(13)(21)(20)(1)(12)(2)(3)(4)(51)JavaBean经典教程_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
文档贡献者
评价文档:
&&¥1.00
&&¥8.00
喜欢此文档的还喜欢
JavaBean经典教程
把文档贴到Blog、BBS或个人站等:
普通尺寸(450*500pix)
较大尺寸(630*500pix)
大小:172.00KB
登录百度文库,专享文档复制特权,财富值每天免费拿!
你可能喜欢

我要回帖

更多关于 javabean是什么 的文章

 

随机推荐