- 浏览: 546293 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
wa114d:
楼主工作几年了,好厉害
一个面试官对面试问题的分析 -
wobuxiaole:
Good,非常好
30岁前男人需要完成的事 -
小逗逗:
Good,非常好
30岁前男人需要完成的事 -
invincibleLiu:
好帖,要顶!(别投我隐藏啊,这是对BBS最原始一种支持)
Java:synchronized修饰符在静态方法与非静态方法上的区别 -
fayedShih:
第三题,不知道对不对
import java.util.con ...
企业牛逼面试题目 高手进来讨论答题
假定我们有如下两个标准的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来完成一些基本的JavaBean和DynaBean操作:
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这些是如何被动态创建的,一定会有收获。
发表评论
-
妙用Commons良药 StringUtils
2011-03-08 10:48 1318来自:http://lighter.iteye.c ... -
分享 commons io 工具类 代码
2011-03-04 17:53 1148来自:http://www.iteye.com/topic/5 ... -
Jakarta Commons笔记] 结语
2008-11-12 11:15 1277在前面的随笔中,我们一起过了一遍Jakarta Common ... -
Commons Collections - Transformer组 补充
2008-11-12 11:11 1290// transformer setup T ... -
[Jakarta Commons笔记] Commons Collections - Closure组
2008-11-12 10:31 1577接下来看Closure组。 Closure Chaine ... -
Commons Collections - Transformer
2008-11-12 10:19 1506接下来看Transformer组。 T ... -
[Jakarta Commons笔记]Commons Collections - Predicate
2008-11-12 10:17 2042接下来看Predicate组 Predicate And ... -
Commons Collections - Comparator组
2008-11-12 09:55 1371接下来我们会讲到辅助类,首先看Comparator组。 R ... -
Commons Collections - Collection 组
2008-11-12 09:43 1320接下来看看Collection组。 TypedColle ... -
[Jakarta Commons笔记] Commons Collections - Map组
2008-11-12 09:39 1574接下来看Map组。 BidiMap MultiMap L ... -
[Jakarta Commons笔记] Commons Collections - Buffer组
2008-11-12 09:23 1403来看Buffer组。 Buffer BlockingBuf ... -
[Jakarta Commons笔记] Commons Collections - Bag组
2008-11-11 17:48 1296先来看Bag组。 Bag HashBag BagUtils ... -
[Jakarta Commons笔记] Commons Collections
2008-11-11 17:30 2342Commons Collections,又是 ... -
[Jakarta Commons笔记] Commons BeanUtils
2008-11-11 16:44 1934Jakarta Commons项目提供了相当丰富的API,我们 ... -
[Jakarta Commons笔记] org.apache.commons.lang.time
2008-11-11 16:14 1818好了,来看我在Common Lang中最后要讲的一个包:org ... -
[Jakarta Commons笔记] org.apache.commons.lang.math
2008-11-11 16:12 1809在Jakarta Commons中,专门处理数学计算的类分别可 ... -
[Jakarta Commons笔记] commons.lang.builder
2008-11-11 15:36 1102在前面的专题文章中,我们一起过了一遍org.apache.c ... -
[Jakarta Commons笔记] 代码范例 - ArrayUtils StringUtils
2008-11-11 15:34 1679数组是我们经常需要使用到的一种数据结构,但是由于Java本身 ... -
[Jakarta Commons笔记] Commons Lang
2008-11-11 15:32 2684跟java.lang这个包的作用类似,Commons Lang ... -
[Jakarta Commons笔记] 开篇
2008-11-11 13:45 1145在Java的世界,有很多(成千上万)开源的框架,有成功的,也 ...
相关推荐
这个库在标题中提及的"commons-beanutils-1.8.3.jar.zip"是一个压缩文件,其中包含了`commons-beanutils-1.8.3.jar`,这是一个用于处理JavaBean属性的库的特定版本。这个版本的BeanUtils提供了许多实用方法,使得...
Commons BeanUtils Most Java developers are used to creating Java classes that conform to the JavaBeans naming patterns for property getters and setters. It is natural to then access these methods ...
commons-beanutils-1.8.3.rar官方正版免费版,BeanUtils主要提供了对于JavaBean进行各种操作。 个包主要提供用于操作JavaBean的工具类,Jakarta-Common-BeanUtils的主要功能都在这个包里实现。
这个"commons-beanutils-1.8.2-bin"全功能包包含了一系列的jar文件,这些文件是BeanUtils库的核心组成部分,允许开发者更简单地处理JavaBeans属性。以下是关于这个包及其包含的内容的详细说明: 1. **BeanUtils库...
在开始JSP上传文件之前你要准备以下几个东西: 1. commons-FileUpload 1.2 包 下载地址:...3. Commons-BeanUtils 1.7 包 下载地址:http://jakarta.apache.org/commons/beanutils/
jakarta-commons 相关依赖包,文件列表: commons-attributes-api.jar commons-attributes-compiler.jar commons-beanutils.jar commons-codec.jar commons-collections.jar commons-dbcp.jar commons-digester.jar ...
本文将详细探讨`commons-beanutils-1.8.0.jar`及其依赖的`commons-logging.jar`,并阐述它们在实际项目中的应用。 首先,`commons-beanutils-1.8.0.jar`是Apache Commons项目的一个子项目,其核心目标是提供一套...
apache-jakarta-commons-codec.jar apache-jakarta-commons-collections.jar apache-jakarta-commons-discovery.jar apache-jakarta-commons-email.jar apache-jakarta-commons-fileupload.jar apache-jakarta-...
赠送源代码:jakarta.validation-api-2.0.2-sources.jar; 赠送Maven依赖信息文件:jakarta.validation-api-2.0.2.pom; 包含翻译后的API文档:jakarta.validation-api-2.0.2-javadoc-API文档-中文(简体)版.zip; ...
赠送源代码:jakarta.xml.bind-api-2.3.3-sources.jar; 赠送Maven依赖信息文件:jakarta.xml.bind-api-2.3.3.pom; 包含翻译后的API文档:jakarta.xml.bind-api-2.3.3-javadoc-API文档-中文(简体)版.zip; Maven...
赠送源代码:jakarta.xml.bind-api-2.3.3-sources.jar; 赠送Maven依赖信息文件:jakarta.xml.bind-api-2.3.3.pom; 包含翻译后的API文档:jakarta.xml.bind-api-2.3.3-javadoc-API文档-中文(简体)-英语-对照版.zip...
赠送源代码:jakarta.annotation-api-1.3.5-sources.jar; 赠送Maven依赖信息文件:jakarta.annotation-api-1.3.5.pom; 包含翻译后的API文档:jakarta.annotation-api-1.3.5-javadoc-API文档-中文(简体)版.zip; ...
赠送源代码:jakarta.activation-api-1.2.1-sources.jar; 赠送Maven依赖信息文件:jakarta.activation-api-1.2.1.pom; 包含翻译后的API文档:jakarta.activation-api-1.2.1-javadoc-API文档-中文(简体)版.zip; ...
在本话题中,我们关注的是三个特定的jar包:jakarta-taglibs-standard-1.1.2、commons-fileupload-1.3.2和commons-io-2.5。 1. **jakarta-taglibs-standard-1.1.2**: 这个jar包是Jakarta Taglibs项目的一部分,...
赠送源代码:jakarta.xml.bind-api-2.3.2-sources.jar; 赠送Maven依赖信息文件:jakarta.xml.bind-api-2.3.2.pom; 包含翻译后的API文档:jakarta.xml.bind-api-2.3.2-javadoc-API文档-中文(简体)版.zip; Maven...
赠送源代码:jakarta.validation-api-2.0.1-sources.jar; 赠送Maven依赖信息文件:jakarta.validation-api-2.0.1.pom; 包含翻译后的API文档:jakarta.validation-api-2.0.1-javadoc-API文档-中文(简体)版.zip; ...
赠送源代码:jakarta.servlet-api-4.0.4-sources.jar; 赠送Maven依赖信息文件:jakarta.servlet-api-4.0.4.pom; 包含翻译后的API文档:jakarta.servlet-api-4.0.4-javadoc-API文档-中文(简体)版.zip; Maven坐标:...
赠送源代码:jakarta.activation-api-1.2.2-sources.jar; 赠送Maven依赖信息文件:jakarta.activation-api-1.2.2.pom; 包含翻译后的API文档:jakarta.activation-api-1.2.2-javadoc-API文档-中文(简体)-英语-对照...
赠送源代码:jakarta.validation-api-2.0.1-sources.jar; 赠送Maven依赖信息文件:jakarta.validation-api-2.0.1.pom; 包含翻译后的API文档:jakarta.validation-api-2.0.1-javadoc-API文档-中文(简体)-英语-对照...