DisplayTag應(yīng)用指南
【摘 要】本文詳細(xì)介紹Web開發(fā)設(shè)計中的:DisplayTag應(yīng)用指南,DisplayTag是一個非常好用的表格顯示標(biāo)簽,適合MVC模式。 DisplayTag是一個非常好用的表格顯示標(biāo)簽,適合MVC模式,其主頁在http://displaytag.sourceforge.net
一、最簡單的情況,未使用<display:column/>標(biāo)簽
<%request.setAttribute( "test", new ReportList(6) );%>
<display:table name="test" />
標(biāo)簽遍歷List里的每一個對象,并將對象里的所有屬性顯示出來。一般用于開發(fā)的時候檢查對象數(shù)據(jù)的完整性。
二、使用<display:column/>標(biāo)簽的情況
<display:table name="test">
<display:column property="id" title="ID" />
<display:column property="name" />
<display:column property="email" />
<display:column property="status" />
<display:column property="description" title="Comments"/>
</display:table>
property對應(yīng)List里對象的屬性(用getXXX()方法取得),title則對應(yīng)表格表頭里的列名。定義列有兩種方式:
A、<display:column property="email" />
使用<display:column/>標(biāo)簽里的property屬性來定義
B、<display:column title="email">email@it.com</display:column>
在<display:column/>標(biāo)簽體里增加內(nèi)容,可以是常量,也可以用其他標(biāo)簽等等
兩種方式比較,用property屬性來定義更加快速和利于排序。
三、表格顯示樣式的定義
A、在<display:table/>和<display:column/>標(biāo)簽里指定標(biāo)準(zhǔn)的html屬性,煩瑣
B、修改樣式表
<display:table name="test" class="mars">
<display:column property="id" title="ID" class="idcol"/>
<display:column property="name" />
<display:column property="email" />
<display:column property="status" class="tableCellError" />
<display:column property="description" title="Comments"/>
</display:table>
通過class屬性來指定所要應(yīng)用的樣式??梢栽谄淠J(rèn)樣式表里(./css/screen.css)直接修改
四、標(biāo)簽取得數(shù)據(jù)的數(shù)據(jù)源
有四種范圍
pageScope
requestScope (默認(rèn)) <display:table name="test2" >
sessionScope <display:table name="sessionScope.holder.list" > 注意,這里要指定范圍,非默認(rèn)
applicationScope
五、通過增加id屬性創(chuàng)建隱含的對象
<display:table name="test" id="testit">
<display:column property="id" title="ID" />
<display:column property="name" />
<display:column title="static value">static</display:column>
<display:column title="row number (testit_rowNum)"><%=pageContext.
getAttribute("testit_rowNum")%></display:column>
<display:column title="((ListObject)testit).getMoney()">
<%=((ListObject)pageContext.
getAttribute("testit")).getMoney()%></display:column>
</display:table>
注意到在<display:table/>里增加了id屬性,這時就在page context里創(chuàng)建了一個隱含對象,指向List里的當(dāng)前對象,
可以通過(ListObject)pageContext.getAttribute("id")來捕獲這個對象。同時還創(chuàng)建了一個id_rowNum對象,同樣,可
通過pageContext.getAttribute("testit_rowNum")來捕獲,它僅僅代表當(dāng)前行的行數(shù)。
有了這兩個隱含對象,就可以通過其他標(biāo)簽來訪問,例如Jstl:
<display:table id="row" name="mylist">
<display:column title="row number" >
<c:out value="${row_rowNum}"/>
</display:column>
<display:column title="name" >
<c:out value="${row.first_name}"/>
<c:out value="${row.last_name}"/>
</display:column>
</display:table>
六、顯示部分?jǐn)?shù)據(jù)
顯示開始五條數(shù)據(jù):通過設(shè)定length屬性
<display:table name="test" length="5">
<display:column property="id" title="ID" />
<display:column property="email" />
<display:column property="status" />
</display:table>
顯示第三到第八條數(shù)據(jù):通過設(shè)定offset和length屬性
<display:table name="test" offset="3" length="5">
<display:column property="id" title="ID" />
<display:column property="email" />
<display:column property="status" />
</display:table>
七、對email和url地址的直接連接
<display:table name="test" >
<display:column property="id" title="ID" />
<display:column property="email" autolink="true" />
<display:column property="url" autolink="true" />
</display:table>
如果要顯示的對象里包含email和url地址,則可以在display:column里直接設(shè)定autolink="true"來直接連接
八、使用裝飾模式轉(zhuǎn)換數(shù)據(jù)顯示(寫自己的 decorator )
A、對整個表格應(yīng)用decorator
<display:table name="test" decorator="org.displaytag.sample.Wrapper" >
<display:column property="id" title="ID" />
<display:column property="email" />
<display:column property="status" />
<display:column property="date" />
<display:column property="money" />
</display:table>
org.displaytag.sample.Wrapper即自己寫的decorator,它要繼承TableDecorator類,看看它的一個方法:
public String getMoney()
{
return this.moneyFormat.format(((ListObject) this.getCurrentRowObject()).getMoney());
}
很明顯,它通過父類的getCurrentRowObject()方法獲得當(dāng)前對象,然后對其getMoney()方法進(jìn)行‘油漆’
B、對單獨(dú)的column應(yīng)用decorator
<display:table name="test">
<display:column property="id" title="ID" />
<display:column property="email" />
<display:column property="status" />
<display:column property="date" decorator="org.displaytag.sample.LongDateWrapper" />
</display:table>
org.displaytag.sample.LongDateWrapper要實(shí)現(xiàn)ColumnDecorator接口,它的方法:
public final String decorate(Object columnValue)
{
Date date = (Date) columnValue;
return this.dateFormat.format(date);
}
顯然,它獲得不了當(dāng)前對象(因?yàn)樗鼘?shí)現(xiàn)的是接口),僅僅是獲得該對象的columnValue,然后‘油漆’
九、創(chuàng)建動態(tài)連接
有兩種方法創(chuàng)建動態(tài)連接:
A、在<display:column/>里通過增加href、paramId、paramName、paramScope、paramProperty屬性
href 基本的URL 地址
paramId 加在URL 地址后的參數(shù)名稱
paramName 數(shù)據(jù)bean的名稱,一般為null(即使用當(dāng)前List里的對象)
paramScope 數(shù)據(jù)bean的范圍,一般為null
paramProperty 數(shù)據(jù)bean的屬性名稱,用來填充URL 地址后的參數(shù)值
<display:table name="sessionScope.details">
<display:column property="id" title="ID" href="details.jsp" paramId="id" />
<display:column property="email" href="details.jsp" paramId="action"
paramName="testparam" paramScope="request" />
<display:column property="status" href="details.jsp"
paramId="id" paramProperty="id" />
</display:table>
這種方法簡便直接,但缺點(diǎn)是無法產(chǎn)生類似details.jsp?id=xx&action=xx的復(fù)合URL
B、應(yīng)用decorator 創(chuàng)建動態(tài)連接:
<display:table name="sessionScope.details" decorator=
"org.displaytag.sample.Wrapper" >
<display:column property="link1" title="ID" />
<display:column property="email" />
<display:column property="link2" title="Actions" />
</display:table>
org.displaytag.sample.Wrapper里的方法:
public String getLink1()
{
ListObject lObject= (ListObject)getCurrentRowObject();
int lIndex= getListIndex();
return "<a href=\"details.jsp?index=" + lIndex + "\">"
+ lObject.getId() + "</a>";
}
public String getLink2()
{
ListObject lObject= (ListObject)getCurrentRowObject();
int lId= lObject.getId();
return "<a href=\"details.jsp?id=" + lId
+ "&action=view\">View</a> | "
+ "<a href=\"details.jsp?id=" + lId
+ "&action=edit\">Edit</a> | "
+ "<a href=\"details.jsp?id=" + lId
+ "&action=delete\">Delete</a>";
}
十、分頁
實(shí)現(xiàn)分頁非常的簡單,增加一個pagesize屬性指定一次想顯示的行數(shù)即可
<display:table name="sessionScope.test" pagesize="10">
<display:column property="id" title="ID" />
<display:column property="name" />
<display:column property="email" />
<display:column property="status" />
</display:table>
十一、排序
排序?qū)崿F(xiàn)也是很簡單,在需要排序的column里增加sortable="true"屬性,headerClass="sortable"僅僅是
指定顯示的樣式。column里的屬性對象要實(shí)現(xiàn)Comparable接口,如果沒有的話可以應(yīng)用decorator
defaultsort="1" 默認(rèn)第一個column排序
defaultorder="descending" 默認(rèn)遞減排序
<display:table name="sessionScope.stest" defaultsort="1"
defaultorder="descending">
<display:column property="id" title="ID" sortable="true"
headerClass="sortable" />
<display:column property="name" sortable="true" headerClass="sortable"/>
<display:column property="email" />
<display:column property="status" sortable="true" headerClass="sortable"/>
</display:table>
注意的是,當(dāng)同時存在分頁時排序僅僅針對的是當(dāng)前頁面,而不是整個List都進(jìn)行排序
十二、column 分組
分組只是需要在column里增加group屬性
<display:table name="test" class="simple">
<display:column property="city" title="CITY" group="1"/>
<display:column property="project" title="PROJECT" group="2"/>
<display:column property="amount" title="HOURS"/>
<display:column property="task" title="TASK"/>
</display:table>
十三、導(dǎo)出數(shù)據(jù)到其他格式(頁面溢出filter??)
在<display:table/>里設(shè)定export="true"
在<display:column/>里設(shè)定media="csv excel xml pdf" 決定該字段在導(dǎo)出到其他格式時被包不包含,不設(shè)定則都包含
<display:setProperty name="export.csv" value="false" />
決定該種格式能不能在頁面中導(dǎo)出
<display:table name="test" export="true" id="currentRowObject">
<display:column property="id" title="ID"/>
<display:column property="email" />
<display:column property="status" />
<display:column property="longDescription"
media="csv excel xml pdf" title="Not On HTML"/>
<display:column media="csv excel" title="URL" property="url"/>
<display:setProperty name="export.pdf" value="true" />
<display:setProperty name="export.csv" value="false" />
</display:table>
十四、配置屬性,覆蓋默認(rèn)
兩種方法:
A、在程序classpath下新建displaytag.properties文件
B、對于單個表格,應(yīng)用<display:setProperty>標(biāo)簽
具體可配置的屬性:http://displaytag.sourceforge.net/configuration.html
十五、一個完整的例子
<display:table name="test" export="true" sort="list" pagesize="8">
<display:column property="city" title="CITY" group="1"
sortable="true" headerClass="sortable"/>
<display:column property="project" title="PROJECT"
group="2" sortable="true" headerClass="sortable"/>
<display:column property="amount" title="HOURS"/>
<display:column property="task" title="TASK"/>
</display:table>
sort="list" 對整個list進(jìn)行排序
導(dǎo)出數(shù)據(jù)到其他格式時,group無效
=====================第二篇===================================
DisplayTag七宗罪
PS:第二種方法還不完善,DisplayTag中仍存在一些Bug.
解決:
更改配置文件displaytag.properties,使用displaytag-export-poi.jar包.更改export.excel.class=org.displaytag.export.ExcelView為export.excel.class=org.displaytag.excel.ExcelHssfView,這樣可以解決中文表格數(shù)據(jù)的問題.對于中文列表名亂碼的問題,必須更改org.displaytag.excel.ExcelHssfView源代碼:
HSSFCell cell = xlsRow.createCell((short) colNum++);
cell.setCellValue(columnHeader);
cell.setCellStyle(headerStyle);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
HSSFCell cell = xlsRow.createCell((short) colNum++);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(columnHeader);
cell.setCellStyle(headerStyle);
PS:導(dǎo)出問題不少,尤其是Excel.建議使用Apache POI自己實(shí)現(xiàn)Excel的導(dǎo)出.
======================第三篇=====================================
碰到的導(dǎo)出excel報表的實(shí)際需求,所獲得的一些學(xué)習(xí)心得與技巧與大家分享. 默認(rèn)的displayTag導(dǎo)出的Excel格式會有中文亂碼,網(wǎng)上大部分文章都說只有改一下,org.displaytag.export.ExcelView類中, public String getMimeType() { return "application/vnd.ms-excel"; //$NON-NLS-1$ } 方法,在方法后面追加;chartset=gbk;其它不盡然,如果這樣改的話在tomcat作為web服務(wù)端的話,如果列表中有中文,很慢很慢才會有導(dǎo)出(不過,我至今尚未在tomcat下導(dǎo)出過中文), 把WEB應(yīng)用布署在JBOSS下面,導(dǎo)出的話,如果數(shù)據(jù)量大的話也會有20-40秒不等,后來到displayTag的官網(wǎng)上,去看它的bugtrack,發(fā)現(xiàn)其實(shí)不盡然。其它displayTag如果在你需要導(dǎo)出excel并且想自已利用apache的 POI的話,要再去到官方網(wǎng)上下載一個displaytag-export-poi-1.1.jar的包,詳細(xì)操作請看http://displaytag.sourceforge.net/11/install.html 其中里面談到,如果,每次導(dǎo)出的excel數(shù)據(jù)總是有緩存的話,可以在web.xml文件中加入如下配置進(jìn)行過濾,這樣就可以導(dǎo)出實(shí)時數(shù)據(jù)了。 Configure the Filter in your web.xml: <filter> <filter-name>ResponseOverrideFilter</filter-name> <filter-class>org.displaytag.filter.ResponseOverrideFilter</filter-class> </filter>And add mappings for the pages that you will intercept, for example: <filter-mapping> <filter-name>ResponseOverrideFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> <filter-mapping> <filter-name>ResponseOverrideFilter</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> 從以上內(nèi)容來看,也只是告訴你,如果要用apache的最新的POI的話,需要把displaytag-export-poi-1.1.jar從官網(wǎng)下載下來,放入你的工程文件中, 并沒有說如何調(diào)用寫自己的所需的excel的報表格式.下面就實(shí)際問題,來討論一下如何讓displayTag導(dǎo)出自己所需的excel格式. 在displayTag所提供的接口類中,導(dǎo)出自已所需的Excel有兩種方式,一種是通過指定的excel模板,一種是對excel的所有的格子,一個個樣式的處理, 后一種方式完全體現(xiàn)了“慢工出細(xì)活”的格言,而前一種方式實(shí)現(xiàn)起來顯然要好用得多,只用讀模板的樣式就行了。如何調(diào)用displayTag對導(dǎo)出自定制的Excel文件 所提供的接口呢,請看下文,(不要急噢!^_^) 要調(diào)用displayTag給Poi所提供的接口操作步驟如下: 1,先要在你的appfuse工程中新建一個類,讓其實(shí)現(xiàn)org.displaytag.export.BinaryExportView接口,其中關(guān)鍵的方法是 public void doExport(OutputStream out) throws JspException { String captionvalue = this.model.getCaption(); // ExcelHssfView1 tempExcelView=new ExcelHssfView1(); // try { // BeanUtils.setProperty(tempExcelView, "action",captionvalue); // } catch (IllegalAccessException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (InvocationTargetException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } if (captionvalue != null) { captionvalue = captionvalue.replaceAll("(\\r)|(\\n)|(\\t)", ""); doExportCommon(out); } else { System.out.println("exec Common"); doExportCommon(out); } } 對這個方法按照你的POI的定制excel報表的的方法,然后實(shí)現(xiàn)它,再把內(nèi)容寫入outputStream中去,基本上就可以了,當(dāng)然如果有上面的 this.model.getCaption()是讀取displayTag的頁面標(biāo)簽<displayTag:caption/>標(biāo)簽中的內(nèi)容,可以在不同的JSP頁面放入不同的caption的值 從而判斷調(diào)用不同的方法,生成不同的excel樣式的報表,靈活性兼而有之. 寫完以上類以后,最好先寫個測試方法,用main或junit工具都成,看看你的調(diào)用poi的邏輯有沒有問題。 當(dāng)以上的類及方法寫完后,就要在你的appfuse工程中找displaytag.properties文件了,一般就在web/class/目錄下,找到后,打開此文件,添加如下一段配置: export.excel.class=org.displaytag.export.excel.ExcelHssfView1 后面一段是你的新建的類的文件的路徑 因?yàn)槭嵌鄧Z言系統(tǒng),所以最好把display_en.properties及display_zh_Cn.properties都加上. 到此就完成了,使用自己的POI來在appfuse中導(dǎo)出指定格式樣式的excel文件.先別急噢,還有更精彩的等著你。這個時候又有一個問題來了,如果你 頁面想要顯示某些列表字段列,而導(dǎo)出的excel文件中又不出現(xiàn)這樣的字段,又該如何處理呢,嘿嘿,請接著看下文。 如果要實(shí)現(xiàn)以上功能請如下操作: 1,調(diào)整頁面上的displayTag標(biāo)簽的參數(shù)值,呵,比如: <display:table name="testList" cellspacing="0" cellpadding="0" requestURI="" sort="list" id="testList" pagesize="5" class="table testList" export="true" defaultsort="1" defaultorder="descending" > <display:caption media="excel">ExportByCommon</display:caption> <display:column title="ID" sortable="true" media="html"> <a href="/aaa.html?id=<c:out value="${testList.id }" /> " target="operationFrame"> <c:out value="${testList.id }" /> </a> </display:column> <display:column title="Status" sortable="true" media="html"> <img src="<c:out value="${testList.status}" />.gif" title="<c:out value="Status:${testList.status}" />"> </display:column> <display:column property="remarks" escapeXml="true" sortable="true" title="Remarks" style="word-wrap: break-word;word-break: break-all; width:90px;"/> <display:footer media="excel"> <c:out value="${aa }"></c:out>| <c:out value="(${bb })"></c:out>| <c:out value="${currentDate }"></c:out> </display:footer> <display:setProperty name="item_name" value="Info"/> <display:setProperty name="items_name" value="Infos"/> <display:setProperty name="export.excel" value="true" /> <display:setProperty name="export.excel.filename" value="<%=exportFileName%>"/> <display:setProperty name="export.csv" value="false" /> <display:setProperty name="export.xml" value="false" /> <display:setProperty name="export.pdf" value="false" /> </display:table> </form> 注意上面用了多種配置需求,可以自已加鏈接,自定義導(dǎo)出名,自定義表頭表尾,自定義是否全排序,自定義樣式等,這些網(wǎng)上都有,就不細(xì)說了, 關(guān)鍵的需求點(diǎn)media的配置參數(shù)噢,如果media="excel"表示只在excel中顯示,如果media=html表示僅在頁面出現(xiàn),沒有此參數(shù)是兩個都顯示噢 其它的格式也類同設(shè)置,到此displayTag的點(diǎn)點(diǎn)心得分享與大家完畢,謝謝大家花費(fèi)時間分享我的快樂!^_^ 噢,上文提到中文問題,用POI后就解決了! |
displayTag作為當(dāng)前還算得上比較流行的表現(xiàn)層工具插件,在sourceForge官方網(wǎng)站(http://displaytag.sourceforge.net/)上,平均每天的訪問量數(shù)以萬計,本文是建立在實(shí)際開發(fā)過程中
==============================第四篇=============================
displaytag-1.1.1之中文(亂碼)解決方案
1,displaytag頁面的漢化:
把displaytag.properties考到項目里,同時復(fù)制一份displaytag.properties,修改文件名displaytag_zh_CN.properties,把文件里面的對應(yīng)條目改成中文即可;
同時,文件里的對應(yīng)兩條配置注意選擇合適的使用,下面是struts的配置
locale.provider=org.displaytag.localization.I18nStrutsAdapter
locale.resolver=org.displaytag.localization.I18nStrutsAdapter
2,excel導(dǎo)出中文內(nèi)容亂碼:
重載類org.displaytag.export.ExcelView,復(fù)寫
public String getMimeType(){
return "application/vnd.ms-excel;charset=gbk"; //$NON-NLS-1$
}
原代碼是return "application/vnd.ms-excel"; //$NON-NLS-1$
修改displaytag_zh_CN.properties中對應(yīng)條目:
export.excel.class=yourpackage.SimpleChineseExcelView
3,Excel導(dǎo)出文件名中文亂碼:
重載類org.displaytag.tags.SetPropertyTag,復(fù)寫
private String value;
public void setValue(String propertyValue){
try{
this.value = new String(propertyValue.getBytes("GBK"),"ISO-8859-1");
}catch(Exception e){
this.value = propertyValue;
}
super.setValue(this.value);
}
修改displaytag.tld對應(yīng)條目
<name>setProperty</name>
<!-- <tag-class>org.displaytag.tags.SetPropertyTag</tag-class> -->
<tag-class>yourpackage.SimpleChineseSetPropertyTag</tag-class>
在jsp中應(yīng)用時
<display:setProperty name="export.excel.filename" value="導(dǎo)出中文名稱.xls"/>
注意,這種解決方案只能解決value的中文名稱,而不能解決bodycontent內(nèi)的中文名稱,如
<display:setProperty name="export.excel.filename">導(dǎo)出菜單.xls</display:setProperty>
4,Excel導(dǎo)出文件名中文亂碼bodycontent中的不完美解決方案
<display:setProperty name="export.excel.filename">
<%=new String("導(dǎo)出菜單.xls".getBytes("GBK"),"ISO-8859-1") %>
</display:setProperty>
這種解決方案之所以稱之為不完美適應(yīng)為它要借助頁面中的java代碼實(shí)現(xiàn)
使用Mesources
<display:setProperty name="export.excel.filename">
<%
MessageResources mrs = (MessageResources)request.getAttribute("org.apache.struts.action.MESSAGE");
String fileName = mrs.getMessage("menu.export.excel.filename");
fileName = new String(fileName.getBytes("GBK"),"ISO-8859-1");
out.print(fileName);
%>
</display:setProperty>
誰有好的解決方案可以回帖~~感激不盡~