目前 Commons 簡(jiǎn)介
目前已經(jīng)開(kāi)發(fā)有 release 出來(lái)的版本有 BeanUtils, Betwixt, CLI, Collections, DBCP, Digester, Discovery, EL, FileUpload, HttpClient, Jelly, Jexl, JXPath, Lang, Latka, Logging, Modeler, Net, Pool, Validator 等等
每個(gè)版本都不太一樣, 隨時(shí)都有更新的可能, 至于還沒(méi)有 release 出來(lái)正式的版本, 有一些項目, 可能也正在 使用了 !! 也是有可能因為其他項目做出來(lái)的一些元件, 可以抽出來(lái)共用的, 例如目前 struts 用的 Resources ( Resource bundle component ) , 也被列入 SandBox 研發(fā)中, 準備 release 更符合所有項目的 組件.
jakarta 為何要有 commons 這個(gè) project 出現, 就是希望大家不用重復開(kāi)發(fā)一樣的組件, 達到 reusable 的 目的 !! 而且他們都有容易使用的特性, 也是各個(gè) jakarta committer 牛人們的精華杰作, 因此, 絕對不能錯 過(guò)這一個(gè) open source project !! 各位親愛(ài)的 java 同胞們 .................
BeanUtils 介紹
當我在選擇要先介紹哪一個(gè)組件, 實(shí)在猶豫了很久, 因為每一個(gè)實(shí)在都是精華, 久久無(wú)法做出決定, 所以呢, 只好按照是否 release 再按照字母的先后, 做一個(gè)排序, 希望大家明白 ....
所謂 BeanUtils 為何要開(kāi)發(fā)呢, 每個(gè)工程師或許在寫(xiě) JavaBean 的時(shí)候, 都會(huì )乖乖地去寫(xiě) getters 和 setters, 就是 getXXX() 及 setXXX() methods, 但是當你的 object 是動(dòng)態(tài)產(chǎn)生的, 也許是用文件, 也許是 其他原因, 那你該如何去存取數據呢 !!
幾個(gè)例子你可能會(huì )用到 BeanUtils, 當然, 這是已經(jīng)存在的項目了
BeanUtils API 介紹
BeanUtils 的 Java API 主要的 package 總共四項
BeanUtils.PropertyUtils 介紹
基本上, 我假設大家對 JavaBean 的開(kāi)發(fā)都沒(méi)有問(wèn)題, 就是對 getters 及 setters 都了解是什么. 先假設, 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 中會(huì )區分為三種 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");
千萬(wàn)要記住, BeanUtils 是要讓你隨心所欲使用, 所以呢 index , mapped 當然都可以這樣使用
Employee employee = ...;
String city = (String) PropertyUtils.getProperty(employee,
"subordinate[3].address(home).city");
BeanUtils.DynaBean and BeanUtils.DynaClass 介紹
所有的 Dynamic JavaBean 都是實(shí)現 DynaBean 或 DynaClass 這兩個(gè) interface, 也可能會(huì )用到 DynaProperty class 來(lái)存取 properties . 我們?yōu)楹我玫?Dynamic JavaBean 呢, 例如, 你從數據庫取出來(lái) 的數據, 有時(shí)候可能是三個(gè)欄位, 有時(shí)候是四個(gè)欄位, 如果我們對于每個(gè) Bean 都要去寫(xiě)一個(gè) class, 就會(huì )很 累, 所以對于每一種 javabean 我們就設定他的屬性有哪些, 接著(zhù)就可以使用 PropertyUtils 來(lái)將他的數值取 出, 如此, 可以減少很多開(kāi)發(fā)工時(shí). 在 Struts 的課程中, 很多人問(wèn)到我, 請問(wèn)每一個(gè) ActionForm 都必須寫(xiě) 成 java 文件嗎, 當然, 不需要的, 否則一個(gè)網(wǎng)頁(yè)一個(gè) ActionForm ( 假設都不一樣 ), 那么, 這么浪費時(shí)間 的工作, 為何還要使用 Struts 來(lái)作為 Framework 呢, 所以我們都是使用 org.apache.struts.action.DynaActionForm!!
MutableDynaClass ( since $1.5 ) 這是蠻新的一個(gè) DynaClass, 是為了動(dòng)態(tài)可以調整 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 的參數, 需要轉換成 正確的數據類(lèi)型, 所以 ConvertUtils 就是來(lái)處理這些動(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
目前支持的類(lèi)型有
Converter myConverter =
new org.apache.commons.beanutils.converter.IntegerConverter();
ConvertUtils.register(myConverter, Integer.TYPE); // Native type
ConvertUtils.register(myConverter, Integer.class); // Wrapper class
聯(lián)系客服