http://www.jetmaven.net/documents/j_mapformInStruts.php
我們知道Struts的ActionForm一直被大家視為缺陷,覺得多余,但我個(gè)人認(rèn)為ActionForm還是有它存在的理由。我們建立ActionForm通常和Web頁(yè)面的Form元素綁定,用于數(shù)據(jù)的收集和校驗(yàn)等。ActionForm的屬性必須聲明,然后才能用于和Web頁(yè)面中,我們經(jīng)常遇到一些屬性不需要全部聲明,如查詢條件等,而且ActionForm的屬性太多時(shí)管理也是個(gè)問題,再另一些情況下,如采購(gòu)單,使用master/detail方式,ActionForm的創(chuàng)建變的困難,好多屬性均不確定,如采購(gòu)明細(xì)為對(duì)條記錄,這樣處理比較麻煩,在這篇文章中,我們將向你講述如何使用Struts的MapForm機(jī)制實(shí)現(xiàn)這樣的功能。
我們希望ActionForm能夠接收Map數(shù)據(jù),這樣我們的管理就變的容易多啦,在Struts 1.1以后版本這樣的處理變得非常簡(jiǎn)單,我們?cè)贏ctionForm中聲明一個(gè)Map變量。
public class MapForm extends ActionForm
{
private Map map = null;
public void setMap(Map map) {
this.map = map;
}
public Map getMap() {
return this.map;
}
同時(shí)增加一個(gè)屬性方法去設(shè)置和獲取Map中的數(shù)據(jù)。
public void setAttribute(String attributeKey, Object attributeValue)
{
getMap().put(attributeKey, attributeValue);
}
public Object getAttribute(String attributeKey)
{
Object keyValue = getMap().get(attributeKey);
return keyValue;
}
這樣我們?cè)趈sp頁(yè)面中,我們就可以使用Struts的標(biāo)簽接觸這些Map數(shù)據(jù)。
<html:text property="attribute(key)"/>
這樣這些數(shù)據(jù)就可維護(hù)啦,這對(duì)查詢條件較多的情況非常適用,你無(wú)需在維護(hù)這些查詢信息在各個(gè)頁(yè)面的過渡,Struts幫您完成了一切。
下面我們就看一下如何用MapForm組織master/detail方式的數(shù)據(jù),我們將以一個(gè)訂單做為樣例。
1 首先建立一個(gè)Form對(duì)象,繼承MapForm,同時(shí)聲明主要的屬性,如訂單編碼、定購(gòu)人等。
public class OrderForm extends MapForm
{
private Integer id;
private String orderMan;
2 我們擬定以Map方式保存采購(gòu)項(xiàng)信息,同一采購(gòu)項(xiàng)采用統(tǒng)一前綴,可選擇行編碼,如row123_ productCode,row123_ productId,row123_ amount等,這樣某一采購(gòu)項(xiàng)信息將被輸入到Map中,不同的采購(gòu)項(xiàng)的前綴不一樣,前綴由row+行編碼組成,同時(shí)編寫可獲取行編碼的函數(shù),這樣可取得某一采購(gòu)項(xiàng)的所有信息,參數(shù)rowPrefix為某一字符串,如“row”、“item”等,不包含數(shù)字編碼信息,同時(shí)你可以編寫Comparator,進(jìn)行排序。
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頁(yè)面中你可以通過jstl,就可以完成采購(gòu)明細(xì)的顯示。
<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中的數(shù)據(jù)值默認(rèn)都是String類型的,如果你想轉(zhuǎn)換成你想要的類型,可以在你的Form編寫一個(gè)工具方法,完成類型的轉(zhuǎn)換。
public Map getTypedMap() {
Map map = this.getMap();
String keyString = (String) map.get("key");
Integer keyInteger = new Integer(keyString);
map.put("key",keyInteger);
return map;
}
總結(jié):MapForm各個(gè)功能很少被開發(fā)人員使用,如果使用得當(dāng),功能非常強(qiáng)大。ActionForm個(gè)人認(rèn)為并非是個(gè)設(shè)計(jì)缺陷,結(jié)合BeanUtils等工具包,轉(zhuǎn)換和信息處理非常方便。