漁人碼頭

          天行健,君子以自強不息。地勢坤,君子以厚德載物。
          posts - 12, comments - 16, trackbacks - 0, articles - 43
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          DisplayTag筆記(轉(zhuǎn))

          Posted on 2008-02-23 09:08 Fisher 閱讀(2067) 評論(0)  編輯  收藏 所屬分類: 開源組件

          DisplayTag筆記

          本文介紹DisplayTag的一些使用方法。摘自[http://hongyu0809.spaces.msn.com/PersonalSpace.aspx]
          DisplayTag 基本介紹
          DisplayTag是一個非常好用的表格顯示標(biāo)簽,適合MVC模式,
          其主頁在http://displaytag.sourceforge.net
          在這里可以下載displaytag-1.0.jar
          同時需要一下包文件:
          jstl.jar
          standard.jar
          commons-lang-2.0.jar
          commons-collections.jar
          commons-beanutils.jar
          crimson.jar
          gnujaxp.jar
          nekohtml.jar(org.cyberneko.html.parsers.DOMFragmentParser等)
          xerces-2.4.0.jar(org.apache.html.dom.HTMLDocumentImpl等)
          poi-2.5.1-final-20040804.jar
          然后把對應(yīng)的jar文件copy到WEB-INF/lib中.
          DisplayTag的使用:
          <display:table id="pres" name="${pagedData.data}" sort="list"
          styleClass="list" export="true"
          requestURI="${request.contextPath}" pagesize="10" defaultsort="3">
          name是要展現(xiàn)的集合數(shù)據(jù).
          id:在<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ù)。
          sort     表示整個list被排序.
          pagesize   表示每頁所要展示的數(shù).
          defaultsort   表示默認(rèn)是按第幾列排序的,注意這里是以1開始計數(shù)的,默認(rèn)從第一列開始排序.
          defaultorder    指定排序方式,defaultorder="descending" 指定默認(rèn)為降序
          styleClass    指定所要應(yīng)用的樣式.
          requestURI
          <display:caption>使用displayTag示例</display:caption>
          display:caption標(biāo)記中間的字符串是用來放到表格上面的標(biāo)題.
          <display:column property="firstName" title="First Name" sortable="true" export="true"/>
          display:column標(biāo)記指定了每列的屬性.
          property   對應(yīng)List里對象的屬性(它是用getXXX()方法獲取)
          title      對應(yīng)表格表頭里的列名
          export="true"              導(dǎo)出文件時導(dǎo)出該列
          sortable="true"            設(shè)定該列可進(jìn)行排序
          分類樣板說明如下:
            
            一、最簡單的情況,未使用<display:column/>標(biāo)簽
            
            <%request.setAttribute( "test", new ReportList(6) );%>
            <display:table name="test" />
            
            標(biāo)簽遍歷List里的每一個對象,并將對象里的所有屬性顯示出來。一般用于開發(fā)的時候檢查對象數(shù)據(jù)的完整性。
            
            二、使用<display:column/>標(biāo)簽的情況
            
            <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對應(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、修改樣式表
            <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屬性來指定所要應(yīng)用的樣式。
            
            四、標(biāo)簽取得數(shù)據(jù)的數(shù)據(jù)源
            
            有四種范圍
            
            pageScope
            requestScope (默認(rèn)) <display:table name="test2" >  相當(dāng)于request.getAttribute( "test2"))
            sessionScope <display:table name="sessionScope.holder.list" > 注意,這里要指定范圍,非默認(rèn)
            applicationScope
            
            五、通過增加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)建了一個隱含對象,
            可以通過(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="${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、對整個表格應(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);
            }
            
            顯然,它獲得不了當(dāng)前對象(因為它實現(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ù)值
            <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="link1" title="ID" />
            <display:column property="email" />
            <display:column property="link2" 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() + "</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>";
            }
            
            十、分頁
            
            實現(xiàn)分頁非常的簡單,增加一個pagesize屬性指定一次想顯示的行數(shù)即可
            
            <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>
            
            十一、排序
            
            排序?qū)崿F(xiàn)也是很簡單,在需要排序的column里增加sortable="true"屬性,headerClass="sortable"僅僅是
            
            指定顯示的樣式。column里的屬性對象要實現(xiàn)Comparable接口,如果沒有的話可以應(yīng)用decorator
            
            defaultsort="1"       默認(rèn)第一個column排序
            defaultorder="descending"  默認(rèn)遞減排序
            <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>
            
            注意的是,當(dāng)同時存在分頁時排序僅僅針對的是當(dāng)前頁面,而不是整個List都進(jìn)行排序
            
            十二、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>
            
            十三、導(dǎo)出數(shù)據(jù)到其他格式(頁面溢出filter??)
            
            在<display:table/>里設(shè)定export="true"
            
            在<display:column/>里設(shè)定media="csv excel xml pdf" 決定該字段在導(dǎo)出到其他格式時被包不包含,不設(shè)定則都包含
            
            <display:setProperty name="export.csv" value="false" />
            
            決定該種格式能不能在頁面中導(dǎo)出
            
            <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>
            
            十四、配置屬性,覆蓋默認(rèn)
            
            兩種方法:
            
            A、在程序classpath下新建displaytag.properties文件所有時區(qū)共用,
                建中文編碼則創(chuàng)建displaytag_zh_CN.properties,放到類路徑下,
                jar包內(nèi)共有兩個默認(rèn)的屬性文件TableTag.properties,message.properties
            B、對于單個表格,應(yīng)用<display:setProperty>標(biāo)簽(<display:setProperty name=... value=...>)
            十五、一個完整的例子
            
            <display:table id="test" name="${test}" export="true" sort="list" pagesize="10">
            <display:column property="city" title="城市" group="1" sortable="true" headerClass="sortable"/>
            <display:column property="name" title="名字" group="2" sortable="true" headerClass="sortable"/>
            <display:column property="age" title="年齡"/>
            </display:table>
            
            sort="list" 對整個list進(jìn)行排序
             十六 其它
           
            1)當(dāng)多個表在一頁顯示時,每個表都想要有分頁、排序、導(dǎo)出等功能時,只需為每個table指定一個不同的ID即可。
            2)增加表頭<display:caption>角色管理</display:caption>
            3)增加表尾  <display:footer><tr><td colspan="6" align="center" >HY示例</td></tr></display:footer>
            4)http和email自動鏈接功能,指定autolink="true"
            5)指定一列顯示的最大長度,避免太長把表格變形 maxLength="10" style="whitespace: nowrap;"
            6)當(dāng)列的值為null,使用nulls="false"屬性把null轉(zhuǎn)為空白
            7)<display:setProperty name="export.excel.filename" value="test.xls"/>設(shè)置導(dǎo)出excel的文件名
            8)<display:setProperty name="basic.show.header" value="false"/> 設(shè)置不顯示表頭
            9)在displaytag.properties里修改paging.banner.item_name=記錄后,出現(xiàn)中文顯示亂碼問題,
            解決辦法:
            org.displaytag.properties.TableProperties
            修改
            private String getProperty(String key)
            {
            return this.properties.getProperty(key);
            }
            為
            private String getProperty(String key)
           {
           String s = null;
           try {
           s = new String(this.properties.getProperty(key).getBytes("8859_1"), "GBK");
           }catch(Exception e) {
           s = null;
           }
           return s;
           }
           10)displaytag導(dǎo)出excel文件中文亂碼的解決辦法
            修改org.displaytag.export.ExcelView.java
            public String getMimeType()
            {
             return "application/vnd.ms-excel;charset=GB2312";
            }
           十七 總結(jié)
            我自己使用過程中,遇到顯示當(dāng)前頁數(shù),頁總數(shù)等文字信息為英文的問題,
            需要把displaytag_zh_CN.properties,放到類路徑下就可以了。
          主站蜘蛛池模板: 青海省| 伽师县| 呼和浩特市| 万山特区| 津市市| 维西| 焦作市| 泸西县| 兴宁市| 武隆县| 陆川县| 怀集县| 乐清市| 涟水县| 涡阳县| 鄂温| 板桥市| 华蓥市| 顺平县| 昌宁县| 长宁区| 兴仁县| 新巴尔虎右旗| 安义县| 马尔康县| 阿拉善左旗| 巴中市| 曲水县| 长治县| 华安县| 阳谷县| 凭祥市| 忻城县| 六枝特区| 西吉县| 新兴县| 修文县| 瑞昌市| 泰州市| 宜兰市| 望谟县|