<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Read Sean

    Read me, read Sean.
    posts - 508, comments - 655, trackbacks - 9, articles - 4

    [Jakarta Commons筆記] 代碼范例 - BeanUtils

    Posted on 2005-08-02 17:57 laogao 閱讀(3345) 評論(1)  編輯  收藏 所屬分類: On Java

     

    假定我們有如下兩個標準的JavaBean

     

    /** Address.java */

     

    package sean.study.commons.beanutils;

     

    public class Address {

     

        private String zipCode;

        private String addr;

        private String city;

        private String country;

     

        public Address() {

        }

     

        public Address(String zipCode, String addr, String city, String country) {

            this.zipCode = zipCode;

            this.addr = addr;

            this.city = city;

            this.country = country;

        }

     

        public String getAddr() {

            return addr;

        }

     

        public void setAddr(String addr) {

            this.addr = addr;

        }

     

        public String getCity() {

            return city;

        }

     

        public void setCity(String city) {

            this.city = city;

        }

     

        public String getCountry() {

            return country;

        }

     

        public void setCountry(String country) {

            this.country = country;

        }

     

        public String getZipCode() {

            return zipCode;

        }

     

        public void setZipCode(String zipCode) {

            this.zipCode = zipCode;

        }

     

    }

     

    /** Customer.java */

     

    package sean.study.commons.beanutils;

     

    public class Customer {

     

        private long id;

        private String name;

        private Address[] addresses;

     

        public Customer() {

        }

     

        public Customer(long id, String name, Address[] addresses) {

            this.id = id;

            this.name = name;

            this.addresses = addresses;

        }

     

        public Address[] getAddresses() {

            return addresses;

        }

     

        public void setAddresses(Address[] addresses) {

            this.addresses = addresses;

        }

     

        public long getId() {

            return id;

        }

     

        public void setId(long id) {

            this.id = id;

        }

     

        public String getName() {

            return name;

        }

     

        public void setName(String name) {

            this.name = name;

        }

     

    }

     

     

    我們來看看通常我們是怎樣利用Commons BeanUtils來完成一些基本的JavaBeanDynaBean操作:

     

    package sean.study.commons.beanutils;

     

    import org.apache.commons.beanutils.BasicDynaBean;

    import org.apache.commons.beanutils.BasicDynaClass;

    import org.apache.commons.beanutils.DynaBean;

    import org.apache.commons.beanutils.DynaProperty;

    import org.apache.commons.beanutils.PropertyUtils;

    import org.apache.commons.lang.StringUtils;

     

    public class BeanUtilsUsage {

     

        public static void main(String[] args) throws Exception {

            demoNormalJavaBeans();

            demoDynaBeans();

        }

     

        public static void demoNormalJavaBeans() throws Exception {

     

            System.out.println(StringUtils.center(" demoNormalJavaBeans ", 40, "="));

           

            // data setup

            Address addr1 = new Address("CA1234", "xxx", "Los Angeles", "USA");

            Address addr2 = new Address("100000", "xxx", "Beijing", "China");

            Address[] addrs = new Address[2];

            addrs[0] = addr1;

            addrs[1] = addr2;

            Customer cust = new Customer(123, "John Smith", addrs);

           

            // accessing the city of first address

            String cityPattern = "addresses[0].city";

            String name = (String) PropertyUtils.getSimpleProperty(cust, "name");

            String city = (String) PropertyUtils.getProperty(cust, cityPattern);

            Object[] rawOutput1 = new Object[] { "The city of customer ", name,

                    "'s first address is ", city, "." };

            System.out.println(StringUtils.join(rawOutput1));

           

            // setting the zipcode of customer's second address

            String zipPattern = "addresses[1].zipCode";

            if (PropertyUtils.isWriteable(cust, zipPattern)) {

                System.out.println("Setting zipcode ...");

                PropertyUtils.setProperty(cust, zipPattern, "200000");

            }

            String zip = (String) PropertyUtils.getProperty(cust, zipPattern);

            Object[] rawOutput2 = new Object[] { "The zipcode of customer ", name,

                    "'s second address is now ", zip, "." };

            System.out.println(StringUtils.join(rawOutput2));

           

            System.out.println();

        }

     

        public static void demoDynaBeans() throws Exception {

     

            System.out.println(StringUtils.center(" demoDynaBeans ", 40, "="));

           

            // creating a DynaBean

            DynaProperty[] dynaBeanProperties = new DynaProperty[] {

                    new DynaProperty("name", String.class),

                    new DynaProperty("inPrice", Double.class), 

                    new DynaProperty("outPrice", Double.class),

            };

            BasicDynaClass cargoClass = new BasicDynaClass("Cargo", BasicDynaBean.class, dynaBeanProperties);

            DynaBean cargo = cargoClass.newInstance();

           

            // accessing a DynaBean

            cargo.set("name", "Instant Noodles");

            cargo.set("inPrice", new Double(21.3));

            cargo.set("outPrice", new Double(23.8));

            System.out.println("name: " + cargo.get("name"));

            System.out.println("inPrice: " + cargo.get("inPrice"));

            System.out.println("outPrice: " + cargo.get("outPrice"));

     

            System.out.println();

        }

     

    }

     

    上述代碼運行結果如下:

     

    ========= demoNormalJavaBeans ==========

    The city of customer John Smith's first address is Los Angeles.

    Setting zipcode ...

    The zipcode of customer John Smith's second address is now 200000.

     

    ============ demoDynaBeans =============

    name: Instant Noodles

    inPrice: 21.3

    outPrice: 23.8

     

    以上代碼簡單說明了一下BeanUtils常見的基本用法,還有很多高階或者更具體的應用及原理,這里無法一一講到,而且有很多筆者也不熟悉、不了解,對BeanUtils的講解就到此吧。如果你有興趣,或者還不是很清楚為什么像這樣動態的或者說松散的訪問JavaBean是有必要的,可以把Struts的源碼拿下來研究一下,看看FormBean以及DynaActionForm這些是如何被動態創建的,一定會有收獲。

     

     

    Feedback

    # re: [Jakarta Commons筆記] 代碼范例 - BeanUtils  回復  更多評論   

    2005-08-21 23:16 by 江南白衣[ITO]
    DynaBean 因為要預先定義DynaProperty,代碼比較重。所以Commons也主推LazyDyanBean多一點。
    主站蜘蛛池模板: 免费看www视频| 久久久亚洲裙底偷窥综合| 中国一级毛片免费看视频| 亚洲国产天堂久久综合网站| 亚洲国产综合第一精品小说| 暖暖在线日本免费中文| 13小箩利洗澡无码视频网站免费| 亚洲国产日韩女人aaaaaa毛片在线| 男女午夜24式免费视频| 亚洲精品无码mv在线观看网站 | 夜夜爽妓女8888视频免费观看| 国产无遮挡吃胸膜奶免费看| 亚洲午夜福利在线视频| 亚洲成AⅤ人影院在线观看| 3344永久在线观看视频免费首页 | 亚洲精品国产精品| 啦啦啦在线免费视频| 国产精品九九久久免费视频| 亚洲中文字幕在线无码一区二区| 最近免费中文在线视频| 免费的黄色的网站| 不卡一卡二卡三亚洲| a级毛片免费全部播放| 亚洲综合久久成人69| 久久久久国产精品免费免费搜索 | 亚洲国产综合91精品麻豆| 四虎免费久久影院| 国产无限免费观看黄网站| 亚洲av日韩av激情亚洲| 亚洲&#228;v永久无码精品天堂久久| 午夜免费1000部| 狠狠入ady亚洲精品| 亚洲国语在线视频手机在线| 亚洲熟妇无码AV在线播放| 四虎永久在线精品免费影视| 国产免费毛不卡片| 日本系列1页亚洲系列| 亚洲乱码在线观看| 亚洲国产精品日韩专区AV| 24小时免费直播在线观看| **aaaaa毛片免费|