posts - 32,  comments - 3,  trackbacks - 0
          <2010年2月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28123456
          78910111213

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          相冊(cè)

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          轉(zhuǎn)載自http://nomandia.javaeye.com/blog/320667

          一、最簡(jiǎn)單的情況,未使用<display:column/> 標(biāo)簽

          Html代碼
          1. <%request.setAttribute( "test", new ReportList(6) );%>  
          2. <display:table name="test" />  

             
          標(biāo)簽遍歷List里的每一個(gè)對(duì)象,并將對(duì)象里的所有屬性顯示出來(lái)。一般用于開發(fā)的時(shí)候檢查對(duì)象數(shù)據(jù)的完整性。

          二、 使用<display:column/>標(biāo)簽的情況

          Html代碼
          1. <display:table name="test">  
          2. <display:column property="id" title="ID" />  
          3. <display:column property="name" />  
          4. <display:column property="email" />  
          5. <display:column property="status" />  
          6. <display:column property="description" title="Comments"/>  
          7. </display:table>  

           
          property對(duì)應(yīng)List里對(duì)象的屬性(用getXXX()方法取得),title則對(duì)應(yīng)表格表頭里的列名。定義列有兩種方式:

          A、<display:column property="email" />

          使 用<display:column/>標(biāo)簽里的property屬性來(lái)定義

          B、<display:column title="email">email@it.com</display:column>

          在<display:column/>標(biāo)簽體里增加內(nèi)容,可以是常量,也可以用其他標(biāo)簽等等

          兩種方式比較,用 property屬性來(lái)定義更加快速和利于排序。

          三、表格顯示樣式的定義

          A、 在<display:table/>和<display:column/>標(biāo)簽里指定標(biāo)準(zhǔn)的html屬性,煩瑣

          B、 修改樣式表

          Html代碼
          1. <display:table name="test" class="mars">  
          2. <display:column property="id" title="ID" class="idcol"/>  
          3. <display:column property="name" />  
          4. <display:column property="email" />  
          5. <display:column property="status" class="tableCellError" />  
          6. <display:column property="description" title="Comments"/>  
          7. </display:table>  

           
          通過(guò)class屬性來(lái)指定所要應(yīng)用的樣式。可以在其默認(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

          五、通過(guò)增加id屬性創(chuàng)建隱含的對(duì)象

          Html代碼
          1. <display:table name="test" id="testit">  
          2. <display:column property="id" title="ID" />  
          3. <display:column property="name" />  
          4. <display:column title="static value">static</display:column>  
          5. <display:column title="row number (testit_rowNum)"><%=pageContext.  
          6. getAttribute("testit_rowNum")%></display:column>  
          7. <display:column title="((ListObject)testit).getMoney()">  
          8. <%=((ListObject)pageContext.  
          9. getAttribute("testit")).getMoney()%></display:column>  
          10. </display:table>  

           
          注意到在<display:table/>里增加了id屬性,這時(shí)就在page context里創(chuàng)建了一個(gè)隱含對(duì)象,指向List里的當(dāng)前對(duì)象,

          可以通過(guò)(ListObject)pageContext.getAttribute("id") 來(lái)捕獲這個(gè)對(duì)象。同時(shí)還創(chuàng)建了一個(gè)id_rowNum對(duì)象,同樣,可

          通過(guò) pageContext.getAttribute("testit_rowNum")來(lái)捕獲,它僅僅代表當(dāng)前行的行數(shù)。

          有了這兩個(gè) 隱含對(duì)象,就可以通過(guò)其他標(biāo)簽來(lái)訪問(wèn),例如Jstl:

          Html代碼
          1. <display:table id="row" name="mylist">  
          2. <display:column title="row number" >  
          3. <c:out value="${row_rowNum}"/>  
          4. </display:column>  
          5. <display:column title="name" >  
          6. <c:out value="${row.first_name}"/>  
          7. <c:out value="${row.last_name}"/>  
          8. </display:column>  
          9. </display:table>  

           
          六、顯示部分?jǐn)?shù)據(jù)

          顯示開始五條數(shù)據(jù):通過(guò)設(shè)定length屬性

          Html代碼
          1. <display:table name="test" length="5">  
          2. <display:column property="id" title="ID" />  
          3. <display:column property="email" />  
          4. <display:column property="status" />  
          5. </display:table>  

           
          顯示第三到第八條數(shù)據(jù):通過(guò)設(shè)定offset和length屬性

          Html代碼
          1. <display:table name="test" offset="3" length="5">  
          2. <display:column property="id" title="ID" />  
          3. <display:column property="email" />  
          4. <display:column property="status" />  
          5. </display:table>  

           
          七、對(duì)email和url地址的直接連接

          Html代碼
          1. <display:table name="test" >  
          2. <display:column property="id" title="ID" />  
          3. <display:column property="email" autolink="true" />  
          4. <display:column property="url" autolink="true" />  
          5. </display:table>  

             
          如果要顯示的對(duì)象里包含email和url地址,則可以在display:column里直接設(shè)定autolink="true"來(lái) 直接連接

          八、使用裝飾模式轉(zhuǎn)換數(shù)據(jù)顯示(寫自己的 decorator)

          A、 對(duì)整個(gè)表格應(yīng)用decorator

          Html代碼
          1. <display:table name="test" decorator="org.displaytag.sample.Wrapper" >  
          2. <display:column property="id" title="ID" />  
          3. <display:column property="email" />  
          4. <display:column property="status" />  
          5. <display:column property="date" />  
          6. <display:column property="money" />  
          7. </display:table>  

           org.displaytag.sample.Wrapper即自己寫的 decorator,它要繼承TableDecorator類,看看它的一個(gè)方法:
          public String getMoney(){
              return this.moneyFormat.format(((ListObject) this.getCurrentRowObject()).getMoney());
          }


          很明顯,它通過(guò)父類的getCurrentRowObject()方法獲得當(dāng)前對(duì)象,然后對(duì)其getMoney()方法進(jìn)行'油漆'

          B、對(duì)單獨(dú)的column應(yīng)用decorator

          Html代碼
          1. <display:table name="test">  
          2. <display:column property="id" title="ID" />  
          3. <display:column property="email" />  
          4. <display:column property="status" />  
          5. <display:column property="date" decorator="org.displaytag.sample.LongDateWrapper" />  
          6. </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)前對(duì)象(因?yàn)樗鼘?shí)現(xiàn)的是接口),僅僅是獲得該對(duì)象的columnValue,然后'油漆'

          九、 創(chuàng)建動(dòng)態(tài)連接

          有兩種方法創(chuàng)建動(dòng)態(tài)連接:

          A、 在<display:column/>里通過(guò)增加href、paramId、paramName、paramScope、 paramProperty屬性

          href    基本的URL 地址
          paramId     加在URL 地址后的參數(shù)名稱
          paramName 數(shù)據(jù)bean的名稱,一般為null(即使用當(dāng)前List里的對(duì)象)
          paramScope 數(shù)據(jù)bean的范圍,一般為null
          paramProperty 數(shù)據(jù)bean的屬性名稱,用來(lái)填充URL 地址后的參數(shù)值

          Html代碼
          1. <display:table name="sessionScope.details">  
          2. <display:column property="id" title="ID" href="details.jsp" paramId="id" />  
          3. <display:column property="email" href="details.jsp" paramId="action"  
          4. paramName="testparam" paramScope="request" />  
          5. <display:column property="status" href="details.jsp"  
          6. paramId="id" paramProperty="id" />  
          7. </display:table>  

             
          這種方法簡(jiǎn)便直接,但缺點(diǎn)是無(wú)法產(chǎn)生類似details.jsp?id=xx&action=xx的復(fù)合URL

          B、 應(yīng)用decorator 創(chuàng)建動(dòng)態(tài)連接:

          Html代碼
          1. <display:table name="sessionScope.details" decorator=  
          2. "org.displaytag.sample.Wrapper" >  
          3. <display:column property="link1" title="ID" />  
          4. <display:column property="email" />  
          5. <display:column property="link2" title="Actions" />  
          6. </display:table>  

           
          org.displaytag.sample.Wrapper里的方法:

          Java代碼
          1. public String getLink1(){  
          2.     ListObject lObject= (ListObject)getCurrentRowObject();  
          3.     int lIndex= getListIndex();  
          4.     return "<a href=\"details.jsp?index=" + lIndex + "\">"+ lObject.getId() + "</a>";  
          5. }  
          6. public String getLink2(){  
          7.     ListObject lObject= (ListObject)getCurrentRowObject();  
          8.     int lId= lObject.getId();  
          9.     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>";  
          10. }  

             
          十、分頁(yè)

          實(shí)現(xiàn)分頁(yè)非常的簡(jiǎn)單,增加一個(gè)pagesize屬性指定一次想顯示 的行數(shù)即可

          Html代碼
          1. <display:table name="sessionScope.test" pagesize="10">  
          2. <display:column property="id" title="ID" />  
          3. <display:column property="name" />  
          4. <display:column property="email" />  
          5. <display:column property="status" />  
          6. </display:table>  

           
          十一、排序
            
          排序?qū)崿F(xiàn)也是很簡(jiǎn)單,在需要排序的column里增加 sortable="true"屬性,headerClass="sortable"僅僅是

          指定顯示的樣式。column里的屬性對(duì) 象要實(shí)現(xiàn)Comparable接口,如果沒(méi)有的話可以應(yīng)用decorator

          defaultsort="1"       默認(rèn)第一 個(gè)column排序
          defaultorder="descending"  默認(rèn)遞減排序

           

          Html代碼
          1. <display:table name="sessionScope.stest" defaultsort="1"  
          2. defaultorder="descending">  
          3. <display:column property="id" title="ID" sortable="true"  
          4. headerClass="sortable" />  
          5. <display:column property="name" sortable="true" headerClass="sortable"/>  
          6. <display:column property="email" />  
          7. <display:column property="status" sortable="true" headerClass="sortable"/>  
          8. </display:table>  

           
          注意的是,當(dāng)同時(shí)存在分頁(yè)時(shí)排序僅僅針對(duì)的是當(dāng)前頁(yè)面,而不是整個(gè)List都進(jìn)行排序

          十二、 column 分組

          分組只是需要在column里增加group屬性

          Html代碼
          1. <display:table name="test" class="simple">  
          2. <display:column property="city" title="CITY" group="1"/>  
          3. <display:column property="project" title="PROJECT" group="2"/>  
          4. <display:column property="amount" title="HOURS"/>  
          5. <display:column property="task" title="TASK"/>  
          6. </display:table>  

           
          十三、導(dǎo)出數(shù)據(jù)到其他格式(頁(yè)面溢出filter??)

          在<display:table/> 里設(shè)定export="true"

          在<display:column/>里設(shè)定media="csv excel xml pdf" 決定該字段在導(dǎo)出到其他格式時(shí)被包不包含,不設(shè)定則都包含

          Html代碼
          1. <display:setProperty name="export.csv" value="false" />  

           
          決定該種格式能不能在頁(yè)面中導(dǎo)出

          Html代碼
          1. <display:table name="test" export="true" id="currentRowObject">  
          2. <display:column property="id" title="ID"/>  
          3. <display:column property="email" />  
          4. <display:column property="status" />  
          5. <display:column property="longDescription"  
          6. media="csv excel xml pdf" title="Not On HTML"/>  
          7. <display:column media="csv excel" title="URL" property="url"/>  
          8. <display:setProperty name="export.pdf" value="true" />  
          9. <display:setProperty name="export.csv" value="false" />  
          10. </display:table>  

           
          十四、配置屬性,覆蓋默認(rèn)
            
          兩種方法:

          A、在程序 classpath下新建displaytag.properties文件

          B、 對(duì)于單個(gè)表格,應(yīng)用<display:setProperty>標(biāo)簽

          具體可配置的屬性:http://displaytag.sourceforge.net/configuration.html


          十五、一個(gè)完整的例子

          Html代碼
          1. <display:table name="test" export="true" sort="list" pagesize="8">  
          2. <display:column property="city" title="CITY" group="1"  
          3. sortable="true" headerClass="sortable"/>  
          4. <display:column property="project" title="PROJECT"  
          5. group="2" sortable="true" headerClass="sortable"/>  
          6. <display:column property="amount" title="HOURS"/>  
          7. <display:column property="task" title="TASK"/>  
          8. </display:table>  

           
          sort="list" 對(duì)整個(gè)list進(jìn)行排序

          導(dǎo)出數(shù)據(jù)到其他格式時(shí),group無(wú)效

          posted on 2010-02-04 20:47 donghang73 閱讀(230) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 林周县| 晴隆县| 建湖县| 堆龙德庆县| 安康市| 万宁市| 运城市| 道孚县| 绥化市| 大同县| 宝山区| 广南县| 屯门区| 革吉县| 肃北| 民乐县| 游戏| 始兴县| 济南市| 安徽省| 冀州市| 澄江县| 海阳市| 黄骅市| 冕宁县| 南川市| 翼城县| 珠海市| 潞城市| 卢氏县| 双柏县| 怀来县| 子洲县| 迁安市| 德清县| 阳朔县| 丰顺县| 昂仁县| 贵阳市| 兴业县| 永州市|