綠野仙棕

          常用鏈接

          統(tǒng)計(jì)

          hibernate技術(shù)

          最新評論

          DisplayTag應(yīng)用(轉(zhuǎn)載)

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

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


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

          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對應(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屬性來定義更加快速和利于排序。
          ??
          三、表格顯示樣式的定義
          ? A、在<display:table/>和<display:column/>標(biāo)簽里指定標(biāo)準(zhǔn)的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屬性來指定所要應(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
          ??
          五、通過增加id屬性創(chuàng)建隱含的對象

          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屬性,這時(shí)就在page context里創(chuàng)建了一個(gè)隱含對象,指向List里的當(dāng)前對象,
          ?? 可以通過(ListObject)pageContext.getAttribute("id")來捕獲這個(gè)對象。同時(shí)還創(chuàng)建了一個(gè)id_rowNum對象,同樣,可
          ?? 通過pageContext.getAttribute("testit_rowNum")來捕獲,它僅僅代表當(dāng)前行的行數(shù)。
          ?? 有了這兩個(gè)隱含對象,就可以通過其他標(biāo)簽來訪問,例如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 >


          ?
          六、顯示部分?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 > ??

          ?

          七、對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"來直接連接
          ?
          八、使用裝飾模式轉(zhuǎn)換數(shù)據(jù)顯示(寫自己的 decorator )
          ? A、對整個(gè)表格應(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類,看看它的一個(gè)方法:
          ??????? public String getMoney()
          ??? {
          ??????? return this.moneyFormat.format(((ListObject) this.getCurrentRowObject()).getMoney());
          ??? }
          ??? 很明顯,它通過父類的getCurrentRowObject()方法獲得當(dāng)前對象,然后對其getMoney()方法進(jìn)行‘油漆’
          ? B、對單獨(dú)的column應(yīng)用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要實(shí)現(xiàn)ColumnDecorator接口,它的方法:
          ??????? public final String decorate(Object columnValue)
          ??? {
          ??????? Date date = (Date) columnValue;
          ??????? return this.dateFormat.format(date);
          ??? }
          ??? 顯然,它獲得不了當(dāng)前對象(因?yàn)樗鼘?shí)現(xiàn)的是接口),僅僅是獲得該對象的columnValue,然后‘油漆’
          ???
          九、創(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ù)值

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


          ??? 這種方法簡便直接,但缺點(diǎn)是無法產(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 ="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

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

          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)也是很簡單,在需要排序的column里增加sortable="true"屬性,headerClass="sortable"僅僅是
          ?? 指定顯示的樣式。column里的屬性對象要實(shí)現(xiàn)Comparable接口,如果沒有的話可以應(yīng)用decorator
          ?? defaultsort="1"????????????? 默認(rèn)第一個(gè)column排序
          ?? defaultorder="descending"??? 默認(rèn)遞減排序

          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 >

          ?注意的是,當(dāng)同時(shí)存在分頁時(shí)排序僅僅針對的是當(dāng)前頁面,而不是整個(gè)List都進(jìn)行排序
          ?
          十二、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

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

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

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

          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 >

          ?

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

          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" 對整個(gè)list進(jìn)行排序
          ?? 導(dǎo)出數(shù)據(jù)到其他格式時(shí),group無效

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


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 新河县| 临武县| 东乌珠穆沁旗| 吴桥县| 浦县| 凌源市| 固阳县| 保康县| 尚志市| 三都| 浮梁县| 安泽县| 拉萨市| 化德县| 册亨县| 桦川县| 磴口县| 鹿泉市| 许昌市| 灵宝市| 南召县| 靖宇县| 稷山县| 凌源市| 靖安县| 仙游县| 铁岭市| 子洲县| 津市市| 平乡县| 汤阴县| 房产| 莆田市| 云南省| 临泽县| 红原县| 新源县| 靖江市| 平昌县| 美姑县| 通渭县|