綠野仙棕

          導航

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          留言簿(6)

          隨筆分類

          文章分類

          收藏夾

          隨筆檔案

          文章檔案

          相冊

          閱讀排行榜

          評論排行榜

          常用鏈接

          統計

          hibernate技術

          最新評論

          DisplayTag應用(轉載)

          ? DisplayTag是一個非常好用的表格顯示標簽,適合MVC模式,其主頁在 http://displaytag.sourceforge.net ?
          一、最簡單的情況,未使用<display:column/>標簽

          ?? <% request.setAttribute(? " test " ,? new ?ReportList( 6 )?); %>
          ??
          < display:table? name ="test" ? />


          ? 標簽遍歷List里的每一個對象,并將對象里的所有屬性顯示出來。一般用于開發的時候檢查對象數據的完整性。
          ?
          二、使用<display:column/>標簽的情況

          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對應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、修改樣式表

          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 >


          ?? 通過class屬性來指定所要應用的樣式。可以在其默認樣式表里(./css/screen.css)直接修改
          ??
          四、標簽取得數據的數據源
          ? 有四種范圍
          ?? pageScope
          ?? requestScope (默認)? <display:table name="test2" >
          ?? sessionScope? <display:table name="sessionScope.holder.list" > 注意,這里要指定范圍,非默認
          ?? applicationScope
          ??
          五、通過增加id屬性創建隱含的對象

          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 .getAttribute("testit_rowNum")% ></ display:column >
          6 ???? < display:column? title ="((ListObject)testit).getMoney()" >< %=((ListObject )pageContext.getAttribute("testit")).getMoney()% ></ display:column >
          7 </ 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

          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要實現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 地址后的參數值

          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" ?paramName ="testparam" ?paramScope ="request" ? />
          4 ?? < display:column? property ="status" ?href ="details.jsp" ?paramId ="id" ?paramProperty ="id" ? />
          5 </ 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里的方法:
          ?

          ?1 public ?String?getLink1()
          ?2 ? {
          ?3 ??ListObject?lObject = ?(ListObject)getCurrentRowObject();
          ?4 ?? int ?lIndex = ?getListIndex();
          ?5 ?? return ? " <a?href=\ " details.jsp ? index = " ?+?lIndex?+? " \ " > " ? + ?lObject.getId()? + ? " </a> " ;
          ?6 ?}

          ?7
          ?8
          ?9 ? public ?String?getLink2()
          10 ? {
          11 ??ListObject?lObject = ?(ListObject)getCurrentRowObject();
          12 ?? int ?lId = ?lObject.getId();
          13
          14 ?? return ? " <a?href=\ " details.jsp ? id = " ?+?lId
          15 ??? + ? " &action=view\ " > View </ a > ? | ? "
          16 ??? + ? " <a?href=\ " details.jsp ? id = " ?+?lId
          17 ??? + ? " &action=edit\ " > Edit </ a > ? | ? "
          18 ??? + ? " <a?href=\ " details.jsp ? id = " ?+?lId
          19 ??? + ? " &action=delete\ " > Delete </ a > " ;
          20 ?}

          21
          22

          十、分頁
          ?? 實現分頁非常的簡單,增加一個pagesize屬性指定一次想顯示的行數即可

          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 >

          十一、排序
          ?? 排序實現也是很簡單,在需要排序的column里增加sortable="true"屬性,headerClass="sortable"僅僅是
          ?? 指定顯示的樣式。column里的屬性對象要實現Comparable接口,如果沒有的話可以應用decorator
          ?? defaultsort="1"????????????? 默認第一個column排序
          ?? defaultorder="descending"??? 默認遞減排序

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

          ?注意的是,當同時存在分頁時排序僅僅針對的是當前頁面,而不是整個List都進行排序
          ?
          十二、column 分組
          ?? 分組只是需要在column里增加group屬性

          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 >
          7

          十三、導出數據到其他格式(頁面溢出filter??)
          ?? 在<display:table/>里設定export="true"
          ?? 在<display:column/>里設定media="csv excel xml pdf" 決定該字段在導出到其他格式時被包不包含,不設定則都包含

          ??? < display:setProperty? name ="export.csv" ?value ="false" ? />

          ? 決定該種格式能不能在頁面中導出

          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" ?media ="csv?excel?xml?pdf" ?title ="Not?On?HTML" />
          6 ?? < display:column? media ="csv?excel" ?title ="URL" ?property ="url" />
          7 ?? < display:setProperty? name ="export.pdf" ?value ="true" ? />
          8 ?? < display:setProperty? name ="export.csv" ?value ="false" ? />
          9 </ display:table >

          ?

          十四、配置屬性,覆蓋默認
          ? 兩種方法:
          ? A、在程序classpath下新建displaytag.properties文件
          ? B、對于單個表格,應用<display:setProperty>標簽
          ? 具體可配置的屬性:
          http://displaytag.sourceforge.net/configuration.html
          ?
          十五、一個完整的例子

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


          ?? sort="list" 對整個list進行排序
          ?? 導出數據到其他格式時,group無效

          posted on 2006-10-13 12:16 土牛小屋 閱讀(508) 評論(0)  編輯  收藏 所屬分類: jsp技術


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 武功县| 冕宁县| 高陵县| 武平县| 桂阳县| 祁连县| 侯马市| 岳阳市| 陵水| 连江县| 兖州市| 禹州市| 麦盖提县| 蒲江县| 会泽县| 麻江县| 商南县| 鹤壁市| 五莲县| 杭州市| 同德县| 宜宾县| 鄢陵县| 文化| 马关县| 恩施市| 沙田区| 延边| 全南县| 二手房| 星子县| 荔波县| 克拉玛依市| 裕民县| 苍山县| 柘荣县| 宜丰县| 黑河市| 凤阳县| 涡阳县| 泰顺县|