☆藍(lán)色夢(mèng)想☆

          世界總是反反覆覆錯(cuò)錯(cuò)落落地飄去 來(lái)不及嘆息 生活不是平平淡淡從從容容的東西 不能放棄
          posts - 57, comments - 5, trackbacks - 0, articles - 0
          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)。一般用于開發(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ù)
            
            顯示開始五條數(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ù)顯示(寫自己的 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即自己寫的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
            
            <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ù)名稱
            paramName    數(shù)據(jù)bean的名稱,一般為null(即使用當(dāng)前List里的對(duì)象)
            paramScope    數(shù)據(jù)bean的范圍,一般為null
            paramProperty  數(shù)據(jù)bean的屬性名稱,用來(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)生類似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接口,如果沒有的話可以應(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í)排序僅僅針對(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ú)效

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 沧源| 扶绥县| 鄂托克前旗| 开鲁县| 柳河县| 安图县| 淮滨县| 无棣县| 兰州市| 香港 | 永平县| 九江市| 道孚县| 卢湾区| 漳平市| 峨眉山市| 瓦房店市| 金阳县| 忻城县| 双桥区| 勐海县| 名山县| 巫山县| 团风县| 长宁区| 绍兴市| 霍林郭勒市| 宜丰县| 肥乡县| 崇礼县| 滦平县| 马山县| 永康市| 丰宁| 商城县| 崇礼县| 荔浦县| 鄂尔多斯市| 营山县| 东宁县| 临沂市|