這裏有一個快速/骯髒的方法,我在同時設計用於將DynaBeans到其中(string)鍵名被強制成小寫地圖:
private Map asLowerCaseIndexedMap(DynaBean bean) {
Map lowerCaseIndexedMap = new HashMap();
DynaProperty[] dynaProperties = bean.getDynaClass().getDynaProperties();
for (int i=0, n=dynaProperties.length; i<n; i++) {
String propertyName = dynaProperties[i].getName();
lowerCaseIndexedMap.put(propertyName.toLowerCase(), bean.get(propertyName));
}
return lowerCaseIndexedMap;
}
可惜這迫使我跳通過其他籃球。雖然我已經正是如此處理豆:
public void header(List<DynaBean> headerColumns) throws WriteException {
for (DynaBean column : headerColumns) {
int col = (Integer)column.get("columnNumber");
int width = (Integer)column.get("columnWidth");
//etcetera
現在我以這種方式這樣做:
public void header(List<DynaBean> headerColumns) throws WriteException {
for (DynaBean column : headerColumns) {
/*
* RowSetDynaClass returns beans with lower case properties by default
* but this is not guaranteed if beans are coming from Hibernate.
*
* Converting from the bean to a map however loses DynaBeans' inherent
* type conversions, which in turn entails the converting back and forth
* from String, to BigDecimal, to int as seen below
*/
Map columnMap = asLowerCaseIndexedMap(column);
int col = (new BigDecimal(columnMap.get("columnnbr").toString())).intValue();
int width = (new BigDecimal(columnMap.get("columnwdth").toString())).intValue();
知道推土機而不是Smooks的,但我沒有引進權威新的圖書館,我的上級都去了假期,他們昨天想要它;)我會發布我已經設計的答案/解決方案,而不會偏離BeanUtils。 – 2009-09-03 15:07:11
只是一個建議:你可能可以發佈你的解決方案作爲問題的答案,但不能作爲對問題的編輯:) – 2009-09-03 15:52:04
嗯,我通常發佈我的解決方案作爲我自己的問題作爲答案之前,但我衝突,因爲那麼看起來我只是「碰撞」了我的問題。所以我這次做了編輯。 – 2009-09-03 15:55:19