免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

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

假定我們有如下兩個標(biāo)準(zhǔn)的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();

    }

 

}

 

上述代碼運行結(jié)果如下:

 

========= 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常見的基本用法,還有很多高階或者更具體的應(yīng)用及原理,這里無法一一講到,而且有很多筆者也不熟悉、不了解,對BeanUtils的講解就到此吧。如果你有興趣,或者還不是很清楚為什么像這樣動態(tài)的或者說松散的訪問JavaBean是有必要的,可以把Struts的源碼拿下來研究一下,看看FormBean以及DynaActionForm這些是如何被動態(tài)創(chuàng)建的,一定會有收獲。
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
利用Jakarta Commons組件beanutils、dbutils簡化JDBC數(shù)據(jù)庫操作(二)
javaBean與Map<String,Object>互轉(zhuǎn)
org.apache.commons.beanutils.PropertyUtils 作用一例
關(guān)于第5章的全部習(xí)題答案總結(jié)?。?! - 《JAVA開發(fā)實戰(zhàn)經(jīng)典》答疑專區(qū) - 魔樂MLDN java學(xué)習(xí)社區(qū)-魔樂科技 JAVA論壇|JAVA培訓(xùn)論壇|MLDN論壇|MLDN社區(qū)|MLDN視頻下載 -JAVA學(xué)習(xí)論壇
java高效監(jiān)控目錄
關(guān)于使用commons-betwixt組件實現(xiàn)xml信息-->Java Bean的轉(zhuǎn)化的一些感受
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服