http://www.jetmaven.net/documents/j_mapformInStruts.php
我們知道Struts的ActionForm一直被大家視為缺陷,覺得多余,但我個人認為ActionForm還是有它存在的理由。我們建立ActionForm通常和Web頁面的Form元素綁定,用于數據的收集和校驗等。ActionForm的屬性必須聲明,然后才能用于和Web頁面中,我們經常遇到一些屬性不需要全部聲明,如查詢條件等,而且ActionForm的屬性太多時管理也是個問題,再另一些情況下,如采購單,使用master/detail方式,ActionForm的創建變的困難,好多屬性均不確定,如采購明細為對條記錄,這樣處理比較麻煩,在這篇文章中,我們將向你講述如何使用Struts的MapForm機制實現這樣的功能。
我們希望ActionForm能夠接收Map數據,這樣我們的管理就變的容易多啦,在Struts 1.1以后版本這樣的處理變得非常簡單,我們在ActionForm中聲明一個Map變量。
public class MapForm extends ActionForm
{
private Map map = null;
public void setMap(Map map) {
this.map = map;
}
public Map getMap() {
return this.map;
}
同時增加一個屬性方法去設置和獲取Map中的數據。
public void setAttribute(String attributeKey, Object attributeValue)
{
getMap().put(attributeKey, attributeValue);
}
public Object getAttribute(String attributeKey)
{
Object keyValue = getMap().get(attributeKey);
return keyValue;
}
這樣我們在jsp頁面中,我們就可以使用Struts的標簽接觸這些Map數據。
<html:text property="attribute(key)"/>
這樣這些數據就可維護啦,這對查詢條件較多的情況非常適用,你無需在維護這些查詢信息在各個頁面的過渡,Struts幫您完成了一切。
下面我們就看一下如何用MapForm組織master/detail方式的數據,我們將以一個訂單做為樣例。
1 首先建立一個Form對象,繼承MapForm,同時聲明主要的屬性,如訂單編碼、定購人等。
public class OrderForm extends MapForm
{
private Integer id;
private String orderMan;
2 我們擬定以Map方式保存采購項信息,同一采購項采用統一前綴,可選擇行編碼,如row123_ productCode,row123_ productId,row123_ amount等,這樣某一采購項信息將被輸入到Map中,不同的采購項的前綴不一樣,前綴由row+行編碼組成,同時編寫可獲取行編碼的函數,這樣可取得某一采購項的所有信息,參數rowPrefix為某一字符串,如“row”、“item”等,不包含數字編碼信息,同時你可以編寫Comparator,進行排序。
public Collection getRowIdList(String rowPrefix)
{
if (map.isEmpty()) return new ArrayList();
Collection allRowId = new TreeSet(new RowIdComparator(rowPrefix));
Iterator allKey = map.keySet().iterator();
while (allKey.hasNext())
{
String key = (String) allKey.next();
if (key.indexOf(rowPrefix) != -1)
{
key = key.substring(0, key.indexOf('_'));
allRowId.add(key);
}
}
return allRowId;
}
3 在jsp頁面中你可以通過jstl,就可以完成采購明細的顯示。
<c:forEach var="rowId" items="${OrderForm.getRowIdList('row')}">
<tr align="center" id="${rowId}" onclick="clickRow()">
<td ><html:text property="attribute(${rowId}_productCode)" size="8" onkeydown="fillProductInfoWithKeyDown('${rowId}',this)" /><html:hidden property="attribute(${rowId}_productId)"/> <a href="javascript:selectproduct('${rowId}')">選擇</a></td>
<td ><html:text property="attribute(${rowId}_productQty)" size="8" /> </td>
<td ><html:text property="attribute(${rowId}_productPrice)" size="8" /></td>
<td ><html:text property="attribute(${rowId}_productName)" readonly="true" size="16" /></td>
<td ><html:text property="attribute(${rowId}_productPackaging)" readonly="true" size="12"/></td>
<td ><html:text property="attribute(${rowId}_productUnit)" size="6" readonly="true" /></td>
</tr>
</c:forEach>
4 這樣Struts幫你完成了所有的信息處理,你需要完成你的保存就可以啦。
提示:Map中的數據值默認都是String類型的,如果你想轉換成你想要的類型,可以在你的Form編寫一個工具方法,完成類型的轉換。
public Map getTypedMap() {
Map map = this.getMap();
String keyString = (String) map.get("key");
Integer keyInteger = new Integer(keyString);
map.put("key",keyInteger);
return map;
}
總結:MapForm各個功能很少被開發人員使用,如果使用得當,功能非常強大。ActionForm個人認為并非是個設計缺陷,結合BeanUtils等工具包,轉換和信息處理非常方便。