隨筆 - 6  文章 - 129  trackbacks - 0
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(14)

          隨筆檔案(6)

          文章分類(lèi)(467)

          文章檔案(423)

          相冊(cè)

          收藏夾(18)

          JAVA

          搜索

          •  

          積分與排名

          • 積分 - 827240
          • 排名 - 49

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          文章出處:http://www.javaresearch.org/article/42943.htm
            DisplayTag是一個(gè)非常好用的表格顯示標(biāo)簽,適合MVC模式,其主頁(yè)在http://displaytag.sourceforge.net  
          一、最簡(jiǎn)單的情況,未使用<display:column/>標(biāo)簽
            <%request.setAttribute( "test", new ReportList(6) );%>
            <display:table name="test" />
            標(biāo)簽遍歷List里的每一個(gè)對(duì)象,并將對(duì)象里的所有屬性顯示出來(lái)。一般用于開(kāi)發(fā)的時(shí)候檢查對(duì)象數(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對(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、修改樣式表
          <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>
             通過(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ì)象
          <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屬性,這時(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:
            <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ù)
             顯示開(kāi)始五條數(shù)據(jù):通過(guò)設(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ù):通過(guò)設(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>  

          七、對(duì)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>
           如果要顯示的對(duì)象里包含email和url地址,則可以在display:column里直接設(shè)定autolink="true"來(lái)直接連接
           
          八、使用裝飾模式轉(zhuǎn)換數(shù)據(jù)顯示(寫(xiě)自己的 decorator )
            A、對(duì)整個(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即自己寫(xiě)的decorator,它要繼承TableDecorator類(lèi),看看它的一個(gè)方法:
                  public String getMoney()
              {
                  return this.moneyFormat.format(((ListObject) this.getCurrentRowObject()).getMoney());
              }
              很明顯,它通過(guò)父類(lèi)的getCurrentRowObject()方法獲得當(dāng)前對(duì)象,然后對(duì)其getMoney()方法進(jìn)行‘油漆’
            B、對(duì)單獨(dú)的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要實(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ù)名稱(chēng)
                paramName        數(shù)據(jù)bean的名稱(chēng),一般為null(即使用當(dāng)前List里的對(duì)象)
                paramScope       數(shù)據(jù)bean的范圍,一般為null
                paramProperty    數(shù)據(jù)bean的屬性名稱(chēng),用來(lái)填充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>  
              這種方法簡(jiǎn)便直接,但缺點(diǎn)是無(wú)法產(chǎn)生類(lèi)似details.jsp?id=xx&action=xx的復(fù)合URL
             B、應(yīng)用decorator 創(chuàng)建動(dò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>";
              }

          十、分頁(yè)
             實(shí)現(xiàn)分頁(yè)非常的簡(jiǎn)單,增加一個(gè)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)也是很簡(jiǎn)單,在需要排序的column里增加sortable="true"屬性,headerClass="sortable"僅僅是
             指定顯示的樣式。column里的屬性對(duì)象要實(shí)現(xiàn)Comparable接口,如果沒(méi)有的話(huà)可以應(yīng)用decorator
             defaultsort="1"              默認(rèn)第一個(gè)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)同時(shí)存在分頁(yè)時(shí)如果不指定sort=list,則排序僅僅針對(duì)的是當(dāng)前頁(yè)面,而不是整個(gè)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ù)到其他格式(頁(yè)面溢出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" />
             決定該種格式能不能在頁(yè)面中導(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文件
            B、對(duì)于單個(gè)表格,應(yīng)用<display:setProperty>標(biāo)簽
            具體可配置的屬性:http://displaytag.sourceforge.net/configuration.html
            
          十五、一個(gè)完整的例子
          <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" 對(duì)整個(gè)list進(jìn)行排序
             導(dǎo)出數(shù)據(jù)到其他格式時(shí),group無(wú)效



          posted on 2007-11-14 20:08 Ke 閱讀(370) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): tag
          主站蜘蛛池模板: 安康市| 佛冈县| 子洲县| 成武县| 宝山区| 新晃| 蕉岭县| 卓尼县| 桦川县| 荥经县| 洛南县| 高邮市| 南通市| 英山县| 开远市| 曲靖市| 龙山县| 沾益县| 双峰县| 灌南县| 开鲁县| 吐鲁番市| 奉新县| 武邑县| 青河县| 南雄市| 喀喇沁旗| 景东| 宁国市| 华蓥市| 城市| 松原市| 会理县| 襄汾县| 阳西县| 达孜县| 海晏县| 天气| 北宁市| 寿光市| 上饶市|