Struts下的MapForm

类别:Java 点击:0 评论:0 推荐:

我们知道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)"/> &nbsp;<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等工具包,转换和信息处理非常方便。

ezerg 编程小语

本文地址:http://com.8s8s.com/it/it14114.htm