Commons-beanutils使用指南
来源:互联网 发布:炫酷个人博客php源码 编辑:程序博客网 时间:2024/06/05 15:20
一、commons-beanutils的简要介绍
commons-beanutils是一组使用程序的集合,它能容易的处理bean及属性.该工具允许你以属性名检索bean属性、根据属性排列bean、将bean转换为Map等等。
二、实例说明
1. Author
package org.abu.commons.beanutils;
import java.util.Date;
public class Author {
private String name;
private String email;
private Date birth;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
}
2.Book
package org.abu.commons.beanutils;
import java.util.Date;
import java.util.List;
public class Book {
private String isbn;
private String bookName;
private List<Author> authors;
private String publisher;
private Date publishDate;
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public List<Author> getAuthors() {
return authors;
}
public void setAuthors(List<Author> authors) {
this.authors = authors;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public Date getPublishDate() {
return publishDate;
}
public void setPublishDate(Date publishDate) {
this.publishDate = publishDate;
}
}
3.测试
package org.abu.commons.beanutils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.beanutils.PropertyUtils;
public class BeanUtilsTest {
private Book book = new Book();
private Book copiedBook = new Book();
private List<Author> authors = new ArrayList<Author>();
public BeanUtilsTest() {
// 定义三个作者
Author author1 = new Author();
Author author2 = new Author();
Author author3 = new Author();
author1.setName("Liky");
author1.setEmail("liky@yahoo.com");
author1.setBirth(new Date());
author2.setName("John");
author2.setEmail("john@yahoo.com");
author2.setBirth(new Date());
author3.setName("Susan");
author3.setEmail("susan@yahoo.com");
author3.setBirth(new Date());
// book属性
authors.add(author1);
authors.add(author2);
authors.add(author3);
book.setAuthors(authors);
book.setIsbn("ISBN 7-096-13964-4");
book.setBookName("Struts2 in Action");
book.setPublisher("O'Reilly Media Inc.");
book.setPublishDate(new Date());
}
public static void main(String[] args) throws Exception {
BeanUtilsTest but = new BeanUtilsTest();
//but.getProperty();
but.copyProperties();
}
/**
* 演示使用PropertyUtils来获得属性,
* 注意获得属性有三种方式,分别是:
* 1.getSimpleProperty 用来获取基本数据类型的属性
* 2.getMappedProperty 用来获取Map类型的属性,
* 例如:Book有一个Map类型的author,而author中有三个key,其中一个是name.
* 那么就可以这样来使用getMappedProperty(book, "author", "name");
* 3.getNestedProperty 用来获取嵌套的属性.
* 也就是说如果Book有一个Author的属性,而Author又是另一个Bean,Author有个属性是name,
* 那么就可以这样来使用getNestedProperty(book,"author.name");
*
* @throws Exception
*/
@SuppressWarnings("unchecked")
public void getProperty() throws Exception {
// 获得Bean的简单属性
String bookName = (String) PropertyUtils.getSimpleProperty(book,
"bookName");
String publisher = (String) PropertyUtils.getSimpleProperty(book,
"publisher");
String isbn = (String) PropertyUtils.getSimpleProperty(book, "isbn");
Date publishDate = (Date) PropertyUtils.getSimpleProperty(book,
"publishDate");
StringBuffer bookString = new StringBuffer();
bookString.append("book:/n").append(
"bookName: " + bookName + "/npublisher: " + publisher
+ "/nisbn: " + isbn + "/npublish date: " + publishDate + "/n/n");
// 获得嵌套的属性,这里的Book中有一个Authors的属性,它是Author这个Bean的列表
// 如果要获得嵌套的属性,那么必须使用getNestedProperty这个方法
System.out.println(bookString);
List<Author> authorList = (List<Author>) PropertyUtils
.getNestedProperty(book, "authors");
StringBuffer authorsString = new StringBuffer();
for (int i = 0; i < authorList.size(); i++) {
Author author = authorList.get(i);
authorsString.append("author" + i + ":/n");
authorsString.append("name:"
+ PropertyUtils.getSimpleProperty(author, "name") + "/n");
authorsString.append("email:"
+ PropertyUtils.getSimpleProperty(author, "email") + "/n");
authorsString.append("birth:"
+ PropertyUtils.getSimpleProperty(author, "birth") + "/n/n");
}
System.out.println(authorsString);
}
/**
* 我们来测试是否book会复制属性给copiedBook.
* @throws Exception
*/
public void copyProperties() throws Exception {
PropertyUtils.copyProperties(copiedBook, book);
StringBuffer sb = new StringBuffer();
sb.append("book:/n").append("bookName: " + copiedBook.getBookName()).append("/npublisher: " + copiedBook.getPublisher()).append("/npublish date: " + copiedBook.getPublishDate());
System.out.println(sb);
}
}
- Commons-beanutils使用指南
- Commons-Beanutils
- commons-beanutils
- apache.commons.beanutils.BeanUtils
- 初用apache.commons.beanutils.BeanUtils
- 初用apache.commons.beanutils.BeanUtils
- org.apache.commons.beanutils.BeanUtils
- 初用apache.commons.beanutils.BeanUtils
- org.apache.commons.beanutils.BeanUtils;
- BeanUtils--org.apache.commons.beanutils.BeanUtils
- Jakarta Commons BeanUtils
- Apache Jakarta Commons Beanutils
- commons-beanutils包
- Jakarta Commons BeanUtils
- Jakarta Commons BeanUtils
- commons-beanutils.jar
- commons-beanutils.jar
- commons-BeanUtils简介
- javascript动态执行js文件
- 简单工厂模式(SimpleFactory Pattern),你要什么我就给你什么
- 关于使用jspSmartUpload控件无法获取表单数据
- 贪心算法——单源最短路径 dijkstra
- 写文件的并发问题
- Commons-beanutils使用指南
- 消息摘要算法简介
- 获得文件夹中的文件列表
- 原来有一个博客这么简单!
- asp.net操作xml
- ASP.Net 获取登陆用户信息方法
- ls 命令可以说是Linux下最常用的命令之一。它有众多的选项,其中有很多是很有用的,你是否熟悉呢?下面列出了 ls 命令的绝大多数选项。
- 杨争管理实践日志---懂得欢庆
- Linux新手必须掌握的概念——挂载点