目前 Commons 簡介
目前已經(jīng)開發(fā)有 release 出來的版本有 BeanUtils, Betwixt, CLI, Collections, DBCP, Digester, Discovery, EL, FileUpload, HttpClient, Jelly, Jexl, JXPath, Lang, Latka, Logging, Modeler, Net, Pool, Validator 等等
每個(gè)版本都不太一樣, 隨時(shí)都有更新的可能, 至于還沒有 release 出來正式的版本, 有一些項(xiàng)目, 可能也正在 使用了 !! 也是有可能因?yàn)槠渌?xiàng)目做出來的一些元件, 可以抽出來共用的, 例如目前 struts 用的 Resources ( Resource bundle component ) , 也被列入 SandBox 研發(fā)中, 準(zhǔn)備 release 更符合所有項(xiàng)目的 組件.
jakarta 為何要有 commons 這個(gè) project 出現(xiàn), 就是希望大家不用重復(fù)開發(fā)一樣的組件, 達(dá)到 reusable 的 目的 !! 而且他們都有容易使用的特性, 也是各個(gè) jakarta committer 牛人們的精華杰作, 因此, 絕對不能錯(cuò) 過這一個(gè) open source project !! 各位親愛的 java 同胞們 .................
BeanUtils 介紹
當(dāng)我在選擇要先介紹哪一個(gè)組件, 實(shí)在猶豫了很久, 因?yàn)槊恳粋€(gè)實(shí)在都是精華, 久久無法做出決定, 所以呢, 只好按照是否 release 再按照字母的先后, 做一個(gè)排序, 希望大家明白 ....
所謂 BeanUtils 為何要開發(fā)呢, 每個(gè)工程師或許在寫 JavaBean 的時(shí)候, 都會乖乖地去寫 getters 和 setters, 就是 getXXX() 及 setXXX() methods, 但是當(dāng)你的 object 是動(dòng)態(tài)產(chǎn)生的, 也許是用文件, 也許是 其他原因, 那你該如何去存取數(shù)據(jù)呢 !!
幾個(gè)例子你可能會用到 BeanUtils, 當(dāng)然, 這是已經(jīng)存在的項(xiàng)目了
BeanUtils API 介紹
BeanUtils 的 Java API 主要的 package 總共四項(xiàng)
BeanUtils.PropertyUtils 介紹
基本上, 我假設(shè)大家對 JavaBean 的開發(fā)都沒有問題, 就是對 getters 及 setters 都了解是什么. 先假設(shè), Employee class
public class Employee {
public Address getAddress(String type);
public void setAddress(String type, Address address);
public Employee getSubordinate(int index);
public void setSubordinate(int index, Employee subordinate);
public String getFirstName();
public void setFirstName(String firstName);
public String getLastName();
public void setLastName(String lastName);
}
在 PropertyUtils 中會區(qū)分為三種 method 狀態(tài)
Employee employee = ...;
String firstName = (String)
PropertyUtils.getSimpleProperty(employee, "firstName");
String lastName = (String)
PropertyUtils.getSimpleProperty(employee, "lastName");
.............
PropertyUtils.setSimpleProperty(employee, "firstName", firstName);
PropertyUtils.setSimpleProperty(employee, "lastName", lastName);
Employee employee = ...;
int index = ...;
String name = "subordinate[" + index + "]";
Employee subordinate = (Employee)
PropertyUtils.getIndexedProperty(employee, name);
Employee employee = ...;
int index = ...;
Employee subordinate = (Employee)
PropertyUtils.getIndexedProperty(employee, "subordinate", index);
Employee employee = ...;
Address address = ...;
PropertyUtils.setMappedProperty(employee, "address(home)", address);
Employee employee = ...;
Address address = ...;
PropertyUtils.setMappedProperty(employee, "address", "home", address);
String city = (String)
PropertyUtils.getNestedProperty(employee, "address(home).city");
千萬要記住, BeanUtils 是要讓你隨心所欲使用, 所以呢 index , mapped 當(dāng)然都可以這樣使用
Employee employee = ...;
String city = (String) PropertyUtils.getProperty(employee,
"subordinate[3].address(home).city");
BeanUtils.DynaBean and BeanUtils.DynaClass 介紹
所有的 Dynamic JavaBean 都是實(shí)現(xiàn) DynaBean 或 DynaClass 這兩個(gè) interface, 也可能會用到 DynaProperty class 來存取 properties . 我們?yōu)楹我玫?Dynamic JavaBean 呢, 例如, 你從數(shù)據(jù)庫取出來 的數(shù)據(jù), 有時(shí)候可能是三個(gè)欄位, 有時(shí)候是四個(gè)欄位, 如果我們對于每個(gè) Bean 都要去寫一個(gè) class, 就會很 累, 所以對于每一種 javabean 我們就設(shè)定他的屬性有哪些, 接著就可以使用 PropertyUtils 來將他的數(shù)值取 出, 如此, 可以減少很多開發(fā)工時(shí). 在 Struts 的課程中, 很多人問到我, 請問每一個(gè) ActionForm 都必須寫 成 java 文件嗎, 當(dāng)然, 不需要的, 否則一個(gè)網(wǎng)頁一個(gè) ActionForm ( 假設(shè)都不一樣 ), 那么, 這么浪費(fèi)時(shí)間 的工作, 為何還要使用 Struts 來作為 Framework 呢, 所以我們都是使用 org.apache.struts.action.DynaActionForm!!
MutableDynaClass ( since $1.5 ) 這是蠻新的一個(gè) DynaClass, 是為了動(dòng)態(tài)可以調(diào)整 properties !
DynaProperty[] props = new DynaProperty[]{
new DynaProperty("address", java.util.Map.class),
new DynaProperty("subordinate", mypackage.Employee[].class),
new DynaProperty("firstName", String.class),
new DynaProperty("lastName", String.class)
};
BasicDynaClass dynaClass = new BasicDynaClass("employee", null, props);
DynaBean employee = dynaClass.newInstance();
employee.set("address", new HashMap());
employee.set("subordinate", new mypackage.Employee[0]);
employee.set("firstName", "Fred");
employee.set("lastName", "Flintstone");
Connection conn = ...;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery
("select account_id, name from customers");
Iterator rows = (new ResultSetDynaClass(rs)).iterator();
while (rows.hasNext()) {
DynaBean row = (DynaBean) rows.next();
System.out.println("Account number is " +
row.get("account_id") +
" and name is " + row.get("name"));
}
rs.close();
stmt.close();
Connection conn = ...; // Acquire connection from pool
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT ...");
RowSetDynaClass rsdc = new RowSetDynaClass(rs);
rs.close();
stmt.close();
...; // Return connection to pool
List rows = rsdc.getRows();
...; // Process the rows as desired
MyBean bean = ...;
DynaBean wrapper = new WrapDynaBean(bean);
String firstName = wrapper.get("firstName");
BeanUtils.ConvertUtils 介紹
在很多情況, 例如 struts framework 中, 就常常用到 request.getParameter 的參數(shù), 需要轉(zhuǎn)換成 正確的數(shù)據(jù)類型, 所以 ConvertUtils 就是來處理這些動(dòng)作.
ConvertUtils().convert(java.lang.Object value)
ConvertUtils().convert(java.lang.String[] values, java.lang.Class clazz)
ConvertUtils().convert(java.lang.String value, java.lang.Class clazz)
HttpServletRequest request = ...;
MyBean bean = ...;
HashMap map = new HashMap();
Enumeration names = request.getParameterNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
map.put(name, request.getParameterValues(name));
}
BeanUtils.populate(bean, map);// it will use ConvertUtils for convertings
目前支持的類型有
Converter myConverter =
new org.apache.commons.beanutils.converter.IntegerConverter();
ConvertUtils.register(myConverter, Integer.TYPE); // Native type
ConvertUtils.register(myConverter, Integer.class); // Wrapper class