displaytag 的使用
displaytag 的使用
displaytag的官方網(wǎng)站http://displaytag.sourceforge.net。
1.DisplayTag標(biāo)簽庫概述
(1)概述
DisplayTag是一個開源的自定義標(biāo)簽庫(Custom Tag lib),他提供了直接而有效的格式化web視圖層數(shù)據(jù)的有效手段。可以在web應(yīng)用的MVC模式中集成DisplayTag到View層,其提供的強(qiáng)大表格格式化功能,用來處理jsp網(wǎng)頁上的Table,可以對的Table進(jìn)行分頁、數(shù)據(jù)導(dǎo)出、分組、對列排序等等,能夠大大減少代碼量。
(2)常用標(biāo)簽
(1) display:table</display: table >
DisplayTag標(biāo)簽庫中最重要最核心的標(biāo)簽,上面的標(biāo)簽都是嵌套在它里面來使用的。其核心作用是將給定的對象以html表格形式來顯示,根據(jù)display:column標(biāo)簽格式化每一個項
(2)display:column</display: column >
顯示表格中的一個行對象的一個屬性。
(3)display:footer</display: footer >?????
嵌套在display:table標(biāo)簽中,在表格的最后一行數(shù)據(jù)之后顯示的信息。
(4) display:caption</display:caption>??
它的作用相當(dāng)于html頁面的標(biāo)簽。模仿的簡單標(biāo)記的html標(biāo)題標(biāo)記。?表標(biāo)記中使用它來顯示標(biāo)題。
2.使用前的準(zhǔn)備工作:
(1)添加所需的標(biāo)簽包
commons-beanutils.jar
commons-collection.jar
commons-lang.jar
displaytag.jar
displaytag-export-poi.jar
itext.jar
jstl.jar
standard.jar
(2)在web.xml下添加一個filter
exportFilter
org.displaytag.filter.ResponseOverrideFilter
(3)在jsp頁面做一個引用:
<%@ taglib uri=“http://displaytag.sf.net/el” prefix=“display” %>
(1)表格顯示樣式的定義
<display:table name=“test” styleClass=“l(fā)ist” cellspacing=“0” cellpadding=“0” defaultsort=“1”>
<display:column property=“id” title=“ID” class=“idcol” sort=“true”/>
<display:column property=“name” url=“detail.jsp” paramId=“id” paramProperty=“id” sort=“true”/>
<display:column property=“email” autolink=“true” sort=“true”/>
<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屬性來定義更加快速和利于排序
(2)標(biāo)簽取得數(shù)據(jù)的數(shù)據(jù)源
有四種范圍:
pageScope
requestScope (默認(rèn)):<display:table name=“test2” >
sessionScope:<display:table name=“sessionScope.holder.list” > 注意,這里要指定范圍,非默認(rèn)
applicationScope
(3)通過增加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="rowrowNum"/></display:column><display:columntitle="name"><c:outvalue="{row_rowNum}"/> </display:column> <display:column title="name" > <c:out value="rowr?owNum"/> </display:column> <display:columntitle="name"> <c:outvalue="{row.first_name}"/>
<c:out value="${row.last_name}"/>
</display:column>
</display:table>
(4)分頁
<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>
如果想對代碼分頁,只需在display:table標(biāo)簽中添加一項pagesize=“每頁顯示行數(shù)”,如
<display:table name=“test” pagesize=“10”/>
(5)顯示部分?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>
(6)對列進(jìn)行排序
display tag可對列進(jìn)行排序,就是點擊列名,對該列的數(shù)據(jù)進(jìn)行排序。你只需對想要排序的列添加 sort=“true” 就OK,如下面的代碼可對前三列進(jìn)行排序。在display:table中添加defaultsort=“列數(shù)”,可默認(rèn)對指定的列排序。
如果table有分頁,Display Tag默認(rèn)只對當(dāng)前頁進(jìn)行排序,如果想對整個list排序,可以在display:table之間添加一段代碼:
<display:setProperty name=“sort.amount” value=“l(fā)ist”/>
(7)導(dǎo)出數(shù)據(jù)
在display:table中添加export=“true”,看看會出現(xiàn)什么!Display Tag默認(rèn)會提供三種數(shù)據(jù)導(dǎo)出方式:CSV、Excel、XML 。
另外Display Tag還可以導(dǎo)出為PDF格式,在http://prdownloads.sourceforge.net/itext/下載一個輔助包iText.jar,copy到lib目錄下,然后在display:table之間添加一段代碼:
<display:setProperty name=“export.pdf” value=“true”/>
(8)對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"來直接連接
(9)使用裝飾模式轉(zhuǎn)換數(shù)據(jù)顯示
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、對單獨的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要實現(xiàn)ColumnDecorator接口,它的方法:
public final String decorate(Object columnValue)
{
Date date = (Date) columnValue;
return this.dateFormat.format(date);
}
(10)創(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>
這種方法簡便直接,但缺點是無法產(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=“l(fā)ink1” title=“ID” />
<display:column property=“email” />
<display:column property=“l(fā)ink2” 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() + “”;
}
public String getLink2()
{
ListObject lObject= (ListObject)getCurrentRowObject();
int lId= lObject.getId();
return "<a href=“details.jsp?id=” + lId
+ “&action=view”>View | "
+ "<a href=“details.jsp?id=” + lId
+ “&action=edit”>Edit | "
+ “<a href=“details.jsp?id=” + lId
+ “&action=delete”>Delete”;
}
(11)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>
(12)統(tǒng)計
可以對分組進(jìn)行統(tǒng)計,也可以對所有行進(jìn)行統(tǒng)計。 ??主要靠TableDecorator類finishRow()返回統(tǒng)計結(jié)果,放到頁面去顯示。
(13)表格中嵌套表格
<dispaly:column ><display:table 表格中的內(nèi)容></display:table>?</display:column> ?
4. displaytag的翻頁機(jī)制
?displaytag的翻頁機(jī)制?這可能是displaytag的局限性了,它的翻頁機(jī)制是這樣的: 如果一個list中有10000個bean,按照它的機(jī)制,如果是第一頁(每頁n條),它會把前n條數(shù)據(jù)取出來,然后再把剩余的10000-n條刪除,當(dāng)你點擊頁面“2”的時候,它再從后臺繞一圈,把第二頁的數(shù)據(jù),也就是把第n+1-2n條記錄取出來,把剩余的刪除。這樣,它實現(xiàn)了翻頁,又防止了內(nèi)存占用過大。 但是,不管怎么說,它還是有一個取出所有條數(shù)的動作的,在極大數(shù)據(jù)量的情況下,有可能造成內(nèi)存溢出。
5. Displaytag屬性配置
前面所說的display:setProperty 是一種改變Display Tag屬性的方法,但是在每個jsp中都要寫太麻煩了。
Display Tag中設(shè)置了很多默認(rèn)的屬性,它有一個專門的屬性文件,是在它的jar包中的displaytag/properties/TableTag.properties想要改變它的默認(rèn)屬性,我們可以在WEB-INF\classes下新建一個文件displaytag.properties,仿照TableTag.properties中屬性的格式設(shè)置需要修改的屬性。
TableTag.properties中的# messages中設(shè)置的是顯示在頁面上的提示信息。默認(rèn)是英文的,我們可以把它改為中文的。不過這里只能使用unicode,就是說中文字符必須轉(zhuǎn)換為 unicode碼,這個可以使用jdk自帶的native2ascii.exe進(jìn)行轉(zhuǎn)換。
簡單使用
首先我們定義一個list
<%
List test = new ArrayList( 6 );
test.add( “Test String 1” );
test.add( “Test String 2” );
test.add( “Test String 3” );
test.add( “Test String 4” );
test.add( “Test String 5” );
test.add( “Test String 6” );
request.setAttribute( “test”, test );
%>
當(dāng)我們想在jsp頁面上顯示這個list時,我們只需要寫一句話
<display:table name=“test” />
display tag會自動生成一個table
如果list是從控制層拋出來的,name可使用EL表達(dá)式表示
<display:table name="${test}" />
這是最簡單的display tag的使用,我們可以給它加上樣式等,也可以定義顯示的列,下面的table顯示復(fù)雜一些
<display:table name=“test” styleClass=“l(fā)ist” cellspacing=“0” cellpadding=“0”>
<display:column property=“id” title=“ID” class=“idcol”/>
<display:column property=“name” />
<display:column property=“email” />
<display:column property=“description” title=“Comments”/>
</display:table>
如果想要給它加個鏈接也很簡單,下面的代碼給name加了連接,并附帶id參數(shù),email也自動連接到mailto:XXX
<display:table name=“test” styleClass=“l(fā)ist” cellspacing=“0” cellpadding=“0”>
<display:column property=“id” title=“ID” class=“idcol”/>
<display:column property=“name” url=“detail.jsp” paramId=“id” paramProperty=“id”/>
<display:column property=“email” autolink=“true”/>
<display:column property=“description” title=“Comments”/>
</display:table>
7.displaytag的優(yōu)缺點
?1,支持表格的嵌套.
?2,支持css這樣就能寫少量地代碼使得程序更簡潔.只好定義好一套css,就能全局范圍內(nèi)使用了.
?3,另外還支持截斷長的字符串,這樣就不用在頁面使用自己的方法了.?
?4,能夠?qū)崿F(xiàn)比較復(fù)雜的邏輯從而產(chǎn)生動態(tài)鏈接。這也勉強(qiáng)所以個好處吧。
?5,能輕松地使用struts的資源文件。使得它可以很好的國際化。
?6,能夠過濾空值。
種功能設(shè)計得很體貼、很實用,效率也不錯。導(dǎo)出、分頁、排序都很方便,外
觀設(shè)計也很便于修改。
缺點:?
?1,分組就使用不了了,統(tǒng)計也無法使用了。只統(tǒng)計當(dāng)前頁是沒有意義的。
?2,排序也變成當(dāng)前頁排序了。沒多少意思。
?3,導(dǎo)出也變成當(dāng)前頁導(dǎo)出了,沒多少意思了。
總結(jié)
以上是生活随笔為你收集整理的displaytag 的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 刺猬理论
- 下一篇: 浏览器数据库IndexedDB