java 列表组件_Jsp页面列表组件框架设计
一起學習
Jsp頁面列表組件框架設計
作者:李俊杰
概述
為了提高開發效率,減少重復的頁面多次開發,提高系統的可配置性和代碼的可復用性,也是為了展示struts、hibernate框架,設計原則是降低顯示邏輯和數據的耦合,達到顯示邏輯和數據完全分離,即相同的數據使用不同的顯示邏輯,無須修改顯示邏輯,只需置換不同的顯示模版即可。
實現機制
其中SturtsAction是具體的需要調用列表的Action類,TempDataMap類是具體的列表數據的封裝類,TempData類是具體的表記錄類,即TempDataMap來適配TempData,把表記錄適配成我們需要的列表顯示類。
調用示例:
Action:
//獲取數據庫數據
listRowCount = query.findPayItemByParam(paramInfo,paramForm.getPageSize(),paramForm.getPageNo());
list = listRowCount.getList();
//調用列表組件
if(list != null && list.size() > 0)
ColDataMgrmgr = new ColDataMgr(getDataMaps(list));
//獲取列表的表列信息,數據信息
ArrayList colMetaInfos = mgr.getColMetaInfos();
ArrayList datas = mgr.getData();
//將列表信息傳到List頁面
request.setAttribute(CoreConstant.WEB_DISPLAY_COL_METAT_INFOS_KEY, colMetaInfos);
request.setAttribute(CoreConstant.WEB_DISPLAY_DATAS_KEY, datas);
同樣道理,給出的數據可以分發給打印組件和導出excel列表組件,根據列表的列信息和數據信息,導出excel和打印。
具體的map類的內容可參照附件ReceiveItemAmountQueryMap.java文件。
獲取顯示列信息:
/**
*返回的list中每個元素是列元信息類對象,列元信息類對象包括列名稱,列顯示名稱,
*列顯示格式,其中列名稱是key
*導出excel和打印是同樣的道理
*/
public ArrayList getColMetaInfos()
{
ArrayList list = new ArrayList();
list.add(new ColMetaInfo("transCode", "單據編號", true, "height="20" align="center" nowrap"));
list.add(new ColMetaInfo("transDate", "單據日期", false, "height="20" align="center" nowrap"));
list.add(new ColMetaInfo("transTypeName", "業務類型", false, "height="20" align="left" nowrap"));
list.add(new ColMetaInfo("receiveOrgName", "收款單位", false, "height="20" align="left" nowrap"));
list.add(new ColMetaInfo("receiveAccountNo", "收款賬戶號", false, "height="20" align="center" nowrap"));
list.add(new ColMetaInfo("receiveBankName", "收款銀行", false, "height="20" align="left" nowrap"));
list.add(new ColMetaInfo("extAccountNo", "付款賬戶", false, "height="20" align="center" nowrap"));
list.add(new ColMetaInfo("extBankName", "付款銀行", false, "height="20" align="left" nowrap"));
list.add(new ColMetaInfo("amount", "金額", false, "height="20" align="right" nowrap"));
list.add(new ColMetaInfo("memo", "摘要", false, "height="20" align="left" nowrap"));
return list;
}
//適配數據,根據數據的類型如日期型,裝換成需要的字符串形式,金額類數據也轉換成相//應的字符串格式,保存到HashMap中。
public HashMap getColData()
{
StringBuffer sb = new StringBuffer("receiveItemAmountQueryAction.do?operation=");
sb.append(CoreConstant.WEB_OPERATION_TYPE_TO_UPDATE_KEY);
sb.append("&id=");
sb.append(String.valueOf(form.getTransApplyItem().getTransApplyID().longValue()));
String link = sb.toString();
HashMap hm = new HashMap();
hm.put("transCode","?" IDataFormat.formatString(form.getTransCode()));
hm.put("transDate", "?" IDateFormat.toDateString(form.getTransDate()));
hm.put("transTypeName", "?" IDataFormat.formatString(form.getTransTypeName()));
hm.put("receiveOrgName", "?" IDataFormat.formatString(form.getReceiveOrgName()));
hm.put("receiveAccountNo", "?" IDataFormat.formatString(form.getReceiveAccountNo()));
hm.put("receiveBankName", "?" IDataFormat.formatString(form.getReceiveBankName()));
hm.put("extAccountNo", "?" IDataFormat.formatString(form.getTransApplyItem().getExtAccountNo()));
hm.put("extBankName", "?" IDataFormat.formatString(form.getTransApplyItem().getExtBankName()));
if (form.getTransApplyItem().getAmount() != null)
{
String sAmount = "?" IDataFormat.formatDisabledAmount(form.getTransApplyItem().getAmount().doubleValue() );
hm.put("amount", sAmount);
}
else
{
hm.put("amount", "0.00");
}
hm.put("memo", "?" IDataFormat.formatString(form.getTransApplyItem().getMemo()));
hm.put("SUPERLINK", link);
return hm;
}
附錄:
DataMap接口:
/**
* @author lijj
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public interface DataMap {
public ArrayList getColMetaInfos();
public ArrayList getExeclColMetaInfos();
public ArrayList getPrintColMetaInfos();
public ArrayList getColSearchs();
public HashMap getQueryData();
public String getSearchUrl();
public HashMap getColData();
}
抽象實現類DefaultDataMap
import java.util.ArrayList;
import java.util.HashMap;
/**
* @author lijj
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public abstract class DefaultDataMap implements DataMap{
public abstract ArrayList getColMetaInfos();
public abstract HashMap getColData();
public ArrayList getExeclColMetaInfos()
{
return new ArrayList();
}
public ArrayList getPrintColMetaInfos()
{
return new ArrayList();
}
public String getSearchUrl()
{
return null;
}
public HashMap getQueryData()
{
return null;
}
public ArrayList getColSearchs()
{
ArrayList retList = new ArrayList();
return retList;
}
}
ColDataMgr管理類:
import java.util.ArrayList;
import java.util.HashMap;
/**
* @author lijj
*
* To change the template for this generated type comment go to Window - Preferences - Java - Code Generation - Code and
* Comments
*/
public class ColDataMgr
{
ArrayListlist= null;
DataMapmap= null;
public ColDataMgr(ArrayList list)
{
this.list = list;
}
public ColDataMgr(DataMap map)
{
this.map = map;
}
public ArrayList getColMetaInfos()
{
if (list != null)
{
return ((DataMap) list.get(0)).getColMetaInfos();
}
else
{
return this.map.getColMetaInfos();
}
}
public ArrayList getExeclColMetaInfos()
{
if (list != null)
{
return ((DataMap) list.get(0)).getExeclColMetaInfos();
}
else
{
return this.map.getExeclColMetaInfos();
}
}
public ArrayList getPrintColMetaInfos()
{
if (list != null)
{
return ((DataMap) list.get(0)).getPrintColMetaInfos();
}
else
{
return this.map.getPrintColMetaInfos();
}
}
public ArrayList getData()
{
ArrayList retList = new ArrayList();
DataMap dataMap = null;
if (list != null)
{
for (int i = 0; i < list.size(); i )
{
dataMap = (DataMap) list.get(i);
retList.add(dataMap.getColData());
}
}
return retList;
}
public ArrayList getSearchInfos()
{
return getDataMap().getColSearchs();
}
public String getSearchUrl()
{
return getDataMap().getSearchUrl();
}
public HashMap getQueryData()
{
return getDataMap().getQueryData();
}
private DataMap getDataMap()
{
if (this.map != null)
{
return map;
}
else
{
return ((DataMap) list.get(0));
}
}
}
列元信息類ColMetaInfo
/**
* @author lijj
*
* To change the template for this generated type comment go to Window - Preferences - Java - Code Generation - Code and
* Comments
*/
public class ColMetaInfo
{
private Stringname;
private StringdisplayName;
private booleanisLink;
private String property;
public ColMetaInfo(String name, String displayName, boolean isLink, String property)
{
this.name = name;
this.displayName = displayName;
this.isLink = isLink;
this.property = property;
}
public String getDisplayName()
{
return displayName;
}
public void setDisplayName(String displayName)
{
this.displayName = displayName;
}
public boolean isLink()
{
return isLink;
}
public void setLink(boolean isLink)
{
this.isLink = isLink;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getProperty()
{
return property;
}
public void setProperty(String property)
{
this.property = property;
}
}
模版文件list.jsp
ArrayList colMetaInfos = (ArrayList) request.getAttribute("colMetaInfos");
ArrayList datas = (ArrayList) request.getAttribute("datas");
int colLen = colMetaInfos.size();
int resultCount = datas.size();
%>
ColMetaInfo colMeta = null;
for (int i = 0; i < colLen; i )
{
colMeta = (ColMetaInfo) colMetaInfos.get(i);
%>
}
%>
HashMap hm = null;
for (int i = 0; i < resultCount; i )
{
%>
hm = (HashMap) datas.get(i);
for (int j = 0; j < colLen; j )
{
colMeta = (ColMetaInfo) colMetaInfos.get(j);
%>
>
if (colMeta.isLink() && hm.get(colMeta.getName()) != null
&& !((String) hm.get(colMeta.getName())).equals(""))
{
%>
}
else
{
%>
}
%>
}
%>
}
%>
//沒有記錄顯示空白行
if (resultCount == 0)
{
%>
for (int i = 0; i < colLen; i )
{
colMeta = (ColMetaInfo) colMetaInfos.get(i);
%>
}
}
%>
Jsp使用列表模版的片斷
開戶申請
新增" name="bt" class=button onClick="JavaScript:to_Add();">
總結
以上是生活随笔為你收集整理的java 列表组件_Jsp页面列表组件框架设计的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java窗口绘图_JAVA-窗口中绘制图
- 下一篇: java文件怎么建立关联_如何创建两个J