网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  j2ee综合:如何实现javabean的属性拷贝     
  文章作者:未知  文章来源:赛迪网技术社区  
  查看:56次  录入:管理员--2008-03-14  
 

在struts的实践过程中,经常两个javabean交换数据的情况,如actionform与数据库中的表相关的bean交换数据。通常情况下要写很多get和set语句,一个个属性依次拷贝。这样的话,如此重复繁重的工作让程序员感觉不到编程的快乐。于是在网上查相关资料知,在apache.org有一个project:common-beanutil,提供的一个beanutil类,这个类有一个静态方法beanutil.copyproperties()实现了该功能。后来我在与java相关的书上知道的java的反射机制(reflect),偿试着并实现了两个javabean的属性拷贝功能。
import java.lang.reflect.*;
/**
@author xiaobo zheng
@date 2005-12-28
*/
public class beanutil2{
       /**
       @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                     methodname1=method1[i].getname();
                     methodfix1=methodname1.substring(3,methodname1.length());
                     if(methodname1.startswith("get")){
                            for(int j=0;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数组存放调用该方法的参数
                                                 continue;                                    
                                          }
                                   }
                            }
                     }
              }
              return obj2;
       }
}
再建一个javabean,并测试
import java.lang.reflect.*;
public class user {
    private string name;
    private string id;  
    public void setname(string name){
        this.name=name;
    }
    public string getname(){
        return this.name;
    }
    public void setid(string id){
        this.id=id;
    }
    public string getid(){
        return this.id;
    }
    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());           
    }
}
编译后并执行输出结果
zxb
3286
成功!
         

 
 
上一篇: 关于java数据对象jdo 2.0查询语言的特点    下一篇: java入门:java语言中timer类的简洁用法
  相关文档
e 属性 11-16
drives 集合 11-16
j2ee web的新变革:apusic operamasks 11-16
深入浅出:优化entity bean的七条准则 12-11
判断当前程序是否正在运行 11-17
about ubb with java has a bug. 11-17
java jformattedtextfield 组件的学习 11-17
使用布局管理器 呈现生动的java程序界面 11-17
jcreator pro 3.0与wtk2.0配置心得 11-17
使用properties类带来的好处 11-16
关于mysql5改密码后不能登录问题的解答 11-17
区分引用类型和原始类型 11-17
爪哇语言观察者模式介绍 11-17
java:md5加密器 11-16
java编程交互管理工具:securejsh新特性 04-14
hashtable简易数据库程序 11-17
推荐两本iava书 11-17
与篮球共舞,尽显模式本色 11-17
size 属性 11-16
jdk源码分析:java.lang.boolean 11-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
厦门(总部):13616026886 福州:0591-87655121
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息