一、最簡單的情況,未使用<display:column/>標籤
<%request.setAttribute( "test", new ReportList(6) );%>
<display:table name="test" />
標籤遍歷List裏的每一個對象,並將對象裏的所有屬性顯示出來。一般用於開發的時候檢查對象數據的完整性。
二、使用<display:column/>標籤的情況
<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對應List裏對象的屬性(用getXXX()方法取得),title則對應表格表頭裏的列名。定義列有兩種方式:
A、<display:column property="email" />
使用<display:column/>標籤裏的property屬性來定義
B、<display:column title="email">[email protected]</display:column>
在<display:column/>標籤體裏增加內容,可以是常量,也可以用其他標籤等等
兩種方式比較,用property屬性來定義更加快速和利於排序。
三、表格顯示樣式的定義
A、在<display:table/>和<display:column/>標籤裏指定標準的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屬性來指定所要應用的樣式。可以在其默認樣式表裏(./css/screen.css)直接修改
四、標籤取得數據的數據源
有四種範圍
pageScope
requestScope (默認) <display:table name="test2" >
sessionScope <display:table name="sessionScope.holder.list" > 注意,這裏要指定範圍,非默認
applicationScope
五、通過增加id屬性創建隱含的對象
<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裏創建了一個隱含對象,指向List裏的當前對象,
可以通過(ListObject)pageContext.getAttribute("id")來捕獲這個對象。同時還創建了一個id_rowNum對象,同樣,可
通過pageContext.getAttribute("testit_rowNum")來捕獲,它僅僅代表當前行的行數。
有了這兩個隱含對象,就可以通過其他標籤來訪問,例如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>
六、顯示部分數據
顯示開始五條數據:通過設定length屬性
<display:table name="test" length="5">
<display:column property="id" title="ID" />
<display:column property="email" />
<display:column property="status" />
</display:table>
顯示第三到第八條數據:通過設定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裏直接設定autolink="true"來直接連接
八、使用裝飾模式轉換數據顯示(寫自己的 decorator )
A、對整個表格應用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()方法獲得當前對象,然後對其getMoney()方法進行‘油漆’
B、對單獨的column應用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要實現ColumnDecorator接口,它的方法:
public final String decorate(Object columnValue)
{
Date date = (Date) columnValue;
return this.dateFormat.format(date);
}
顯然,它獲得不了當前對象(因爲它實現的是接口),僅僅是獲得該對象的columnValue,然後‘油漆’
九、創建動態連接
有兩種方法創建動態連接:
A、在<display:column/>裏通過增加href、paramId、paramName、paramScope、paramProperty屬性
href 基本的URL 地址
paramId 加在URL 地址後的參數名稱
paramName 數據bean的名稱,一般爲null(即使用當前List裏的對象)
paramScope 數據bean的範圍,一般爲null
paramProperty 數據bean的屬性名稱,用來填充URL 地址後的參數值
<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>
這種方法簡便直接,但缺點是無法產生類似details.jsp?id=xx&action=xx的複合URL
B、應用decorator 創建動態連接:
<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>";
}
十、分頁
實現分頁非常的簡單,增加一個pagesize屬性指定一次想顯示的行數即可
但是默認的分頁是一次性讀取數據,對於數量級大的表,則應該自行實現分頁代碼爲好!
<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>
十一、排序
排序實現也是很簡單,在需要排序的column裏增加sortable="true"屬性,headerClass="sortable"僅僅是
指定顯示的樣式。column裏的屬性對象要實現Comparable接口,如果沒有的話可以應用decorator
defaultsort="1" 默認第一個column排序
defaultorder="descending" 默認遞減排序
<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>
注意的是,當同時存在分頁時排序僅僅針對的是當前頁面,而不是整個List都進行排序
十二、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>
十三、導出數據到其他格式(頁面溢出filter??)
在<display:table/>裏設定export="true"
在<display:column/>裏設定media="csv excel xml pdf" 決定該字段在導出到其他格式時被包不包含,不設定則都包含
<display:setProperty name="export.csv" value="false" />
決定該種格式能不能在頁面中導出
<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>
十四、配置屬性,覆蓋默認
兩種方法:
A、在程序classpath下新建displaytag.properties文件
B、對於單個表格,應用<display:setProperty>標籤
具體可配置的屬性: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進行排序
導出數據到其他格式時,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,這樣可以解決中文表格數據的問題.對於中文列表名亂碼的問題,必須更改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:導出問題不少,尤其是Excel.建議使用Apache POI自己實現Excel的導出.
======================第三篇=====================================
displayTag作爲當前還算得上比較流行的表現層工具插件,在sourceForge官方網站(http://displaytag.sourceforge.net/)上,平均每天的訪問量數以萬計,本文是建立在實際開發過程中
碰到的導出excel報表的實際需求,所獲得的一些學習心得與技巧與大家分享. 默認的displayTag導出的Excel格式會有中文亂碼,網上大部分文章都說只有改一下,org.displaytag.export.ExcelView類中, public String getMimeType() { return "application/vnd.ms-excel"; //$NON-NLS-1$ } 方法,在方法後面追加;chartset=gbk;其它不盡然,如果這樣改的話在tomcat作爲web服務端的話,如果列表中有中文,很慢很慢纔會有導出(不過,我至今尚未在tomcat下導出過中文), 把WEB應用佈署在JBOSS下面,導出的話,如果數據量大的話也會有20-40秒不等,後來到displayTag的官網上,去看它的bugtrack,發現其實不盡然。其它displayTag如果在你需要導出excel並且想自已利用apache的 POI的話,要再去到官方網上下載一個displaytag-export-poi-1.1.jar的包,詳細操作請看http://displaytag.sourceforge.net/11/install.html 其中裏面談到,如果,每次導出的excel數據總是有緩存的話,可以在web.xml文件中加入如下配置進行過濾,這樣就可以導出實時數據了。 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> 從以上內容來看,也只是告訴你,如果要用apache的最新的POI的話,需要把displaytag-export-poi-1.1.jar從官網下載下來,放入你的工程文件中, 並沒有說如何調用寫自己的所需的excel的報表格式.下面就實際問題,來討論一下如何讓displayTag導出自己所需的excel格式. 在displayTag所提供的接口類中,導出自已所需的Excel有兩種方式,一種是通過指定的excel模板,一種是對excel的所有的格子,一個個樣式的處理, 後一種方式完全體現了“慢工出細活”的格言,而前一種方式實現起來顯然要好用得多,只用讀模板的樣式就行了。如何調用displayTag對導出自定製的Excel文件 所提供的接口呢,請看下文,(不要急噢!^_^) 要調用displayTag給Poi所提供的接口操作步驟如下: 1,先要在你的appfuse工程中新建一個類,讓其實現org.displaytag.export.BinaryExportView接口,其中關鍵的方法是 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報表的的方法,然後實現它,再把內容寫入outputStream中去,基本上就可以了,當然如果有上面的 this.model.getCaption()是讀取displayTag的頁面標籤<displayTag:caption/>標籤中的內容,可以在不同的JSP頁面放入不同的caption的值 從而判斷調用不同的方法,生成不同的excel樣式的報表,靈活性兼而有之. 寫完以上類以後,最好先寫個測試方法,用main或junit工具都成,看看你的調用poi的邏輯有沒有問題。 當以上的類及方法寫完後,就要在你的appfuse工程中找displaytag.properties文件了,一般就在web/class/目錄下,找到後,打開此文件,添加如下一段配置: export.excel.class=org.displaytag.export.excel.ExcelHssfView1 後面一段是你的新建的類的文件的路徑 因爲是多國語言系統,所以最好把display_en.properties及display_zh_Cn.properties都加上. 到此就完成了,使用自己的POI來在appfuse中導出指定格式樣式的excel文件.先別急噢,還有更精彩的等着你。這個時候又有一個問題來了,如果你 頁面想要顯示某些列表字段列,而導出的excel文件中又不出現這樣的字段,又該如何處理呢,嘿嘿,請接着看下文。 如果要實現以上功能請如下操作: 1,調整頁面上的displayTag標籤的參數值,呵,比如: <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> 注意上面用了多種配置需求,可以自已加鏈接,自定義導出名,自定義表頭表尾,自定義是否全排序,自定義樣式等,這些網上都有,就不細說了, 關鍵的需求點media的配置參數噢,如果media="excel"表示只在excel中顯示,如果media=html表示僅在頁面出現,沒有此參數是兩個都顯示噢 其它的格式也類同設置,到此displayTag的點點心得分享與大家完畢,謝謝大家花費時間分享我的快樂!^_^ 噢,上文提到中文問題,用POI後就解決了! |
==============================第四篇=============================
displaytag-1.1.1之中文(亂碼)解決方案
1,displaytag頁面的漢化:
把displaytag.properties考到項目裏,同時複製一份displaytag.properties,修改文件名displaytag_zh_CN.properties,把文件裏面的對應條目改成中文即可;
同時,文件裏的對應兩條配置注意選擇合適的使用,下面是struts的配置
locale.provider=org.displaytag.localization.I18nStrutsAdapter
locale.resolver=org.displaytag.localization.I18nStrutsAdapter
2,excel導出中文內容亂碼:
重載類org.displaytag.export.ExcelView,複寫
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中對應條目:
export.excel.class=yourpackage.SimpleChineseExcelView
3,Excel導出文件名中文亂碼:
重載類org.displaytag.tags.SetPropertyTag,複寫
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對應條目
<name>setProperty</name>
<!-- <tag-class>org.displaytag.tags.SetPropertyTag</tag-class> -->
<tag-class>yourpackage.SimpleChineseSetPropertyTag</tag-class>
在jsp中應用時
<display:setProperty name="export.excel.filename" value="導出中文名稱.xls"/>
注意,這種解決方案只能解決value的中文名稱,而不能解決bodycontent內的中文名稱,如
<display:setProperty name="export.excel.filename">導出菜單.xls</display:setProperty>
4,Excel導出文件名中文亂碼bodycontent中的不完美解決方案
<display:setProperty name="export.excel.filename">
<%=new String("導出菜單.xls".getBytes("GBK"),"ISO-8859-1") %>
</display:setProperty>
這種解決方案之所以稱之爲不完美適應爲它要藉助頁面中的java代碼實現
使用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>