如何将属性从bean复制到另一个类中的另一个bean? [英] How to copy properties from a bean to another bean in different class?

查看:689
本文介绍了如何将属性从bean复制到另一个类中的另一个bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个具有相同属性名称的java类。如何将所有属性复制到另一个填充了data的bean。我不想使用传统的表单来复制属性,因为我有很多属性。

I have two java class with same properties names.How Can I copy all the properties to another bean filled with data.I don't want to use the traditional form to copy properties because I have a lot of properties.

提前致谢。

1级

@ManagedBean
@SessionScoped
public class UserManagedBean implements Serializable {

    private static final long serialVersionUID = 1L;
    private String userSessionId;
    private String userId;
    private String name;
    private String adress;
    ......................

2类

public class UserBean {

    private String userSessionId;
    private String userId;
    private String name;
   ....................


推荐答案

使用 BeanUtils

import org.apache.commons.beanutils.BeanUtils;

UserBean newObject = new UserBean(); 
BeanUtils.copyProperties(newObject, oldObject);

这篇关于如何将属性从bean复制到另一个类中的另一个bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆