一、最簡單的情況,未使用<display:column/>標簽
Html代碼
< %request.setAttribute( "test", new ReportList(6) );%> ? < display:table name ="test" /> ?
<%request.setAttribute( "test", new ReportList(6) );%>
<display:table name="test" />
標簽遍歷List里的每一個對象,并將對象里的所有屬性顯示出來。一般用于開發的時候檢查對象數據的完整性。 二、使用<display:column/>標簽的情況
Html代碼
< 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 > ?
<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@it.com</display:column> 在<display:column/>標簽體里增加內容,可以是常量,也可以用其他標簽等等 兩種方式比較,用property屬性來定義更加快速和利于排序。 三、表格顯示樣式的定義 A、在<display:table/>和<display:column/>標簽里指定標準的html屬性,煩瑣 B、修改樣式表
Html代碼
< 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 > ?
<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屬性創建隱含的對象
Html代碼
< 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 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:
Html代碼
< 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 > ?
<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屬性
Html代碼
< display:table name ="test" length ="5" > ? < display:column property ="id" title ="ID" /> ? < display:column property ="email" /> ? < display:column property ="status" /> ? </ display:table > ?
<display:table name="test" length="5">
<display:column property="id" title="ID" />
<display:column property="email" />
<display:column property="status" />
</display:table>
顯示第三到第八條數據:通過設定offset和length屬性
Html代碼
< display:table name ="test" offset ="3" length ="5" > ? < display:column property ="id" title ="ID" /> ? < display:column property ="email" /> ? < display:column property ="status" /> ? </ display:table > ?
<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地址的直接連接
Html代碼
< display:table name ="test" > ? < display:column property ="id" title ="ID" /> ? < display:column property ="email" autolink ="true" /> ? < display:column property ="url" autolink ="true" /> ? </ display:table > ?
<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
Html代碼
< 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 > ?
<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
Html代碼
< 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 > ?
<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 地址后的參數值
Html代碼
< 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 > ?
<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 創建動態連接:
Html代碼
< 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 > ?
<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里的方法:
Java代碼
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>" ;? }?
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屬性指定一次想顯示的行數即可
Html代碼
< 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 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" 默認遞減排序
Html代碼
< 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 > ?
<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屬性
Html代碼
< 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 > ?
<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" 決定該字段在導出到其他格式時被包不包含,不設定則都包含
Html代碼
< display:setProperty name ="export.csv" value ="false" /> ?
<display:setProperty name="export.csv" value="false" />
決定該種格式能不能在頁面中導出
Html代碼
< 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 > ?
<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
十五、一個完整的例子
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 > ?
<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 详解 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。