使用Display Tag的方法
使用Display Tag的方法


									Display Tag Lib是一个标签库,用来处理jsp网页上的Table,功能非常强,可以对的Table进行分页、数据导出、分组、对列排序等等,反正我在做项目时需要的功能它都给我提供了,而且使用起来非常的方便。能够大大减少代码量。
									介个是Display Tag的官方网站http://displaytag.sourceforge.net。
首先当然是要下载它的jar包了,这里可以下载到最新的版本。将jar包放到WEB-INF的lib文件夹下。另外还需要两个辅助包:apache的commons-lang和standard包,更多的辅助包可以在这里下载。
									在web.xml下添加一个filter
									<filter>
									<filter-name>exportFilter</filter-name>
									<filter-class>org.displaytag.filter.ResponseOverrideFilter</filter-class>
									</filter>
									在jsp页面做一个引用:
									<%@ taglib uri="http://displaytag.sf.net/el" prefix="display" %>
									首先我们定义一个list
									<%
									List test = new ArrayList( 6 );
									test.add( "Test String 1" );
									test.add( "Test String 2" );
									test.add( "Test String 3" );
									test.add( "Test String 4" );
									test.add( "Test String 5" );
									test.add( "Test String 6" );
									request.setAttribute( "test", test );
									%>
									当我们想在jsp页面上显示这个list时,我们只需要写一句话
									<display:table />
									display tag会自动生成一个table
									如果list是从控制层抛出来的,name可使用EL表达式表示
									<display:table />
									这是最简单的display tag的使用,我们可以给它加上样式等,也可以定义显示的列,下面的table显示复杂一些
									<display:table styleClass="list" cellspacing="0" cellpadding="0">
									<display:column property="id" title="ID" />
									<display:column property="name" />
									<display:column property="email" />
									<display:column property="description" title="Comments"/>
									</display:table>
									如果想要给它加个链接也很简单,下面的代码给name加了连接,并附带id参数,email也自动连接到mailto:XXX
									<display:table styleClass="list" cellspacing="0" cellpadding="0">
									<display:column property="id" title="ID" />
									<display:column property="name" url="detail.jsp" paramId="id" paramProperty="id"/>
									<display:column property="email" autolink="true"/>
									<display:column property="description" title="Comments"/>
									</display:table>
									下面介绍几个Display最常用的功能,更多功能请参考http://www.displaytag.org/index.jsp。
									1. 分页
									如果想对代码分页,只需在display:table标签中添加一项pagesize="每页显示行数",如
									<display:table pagesize="10"/>
									2. 对列排序
									display tag可对列进行排序,就是点击列名,对该列的数据进行排序。你只需对想要排序的列添加 sort="true" 就OK,如下面的代码可对前三列进行排序。在display:table中添加defaultsort="列数",可默认对指定的列排序。
									<display:table styleClass="list" cellspacing="0" cellpadding="0" defaultsort="1">
									<display:column property="id" title="ID" sort="true"/>
									<display:column property="name" url="detail.jsp" paramId="id" paramProperty="id" sort="true"/>
									<display:column property="email" autolink="true" sort="true"/>
									<display:column property="description" title="Comments"/>
									</display:table>
									如果table有分页,Display Tag默认只对当前页进行排序,如果想对整个list排序,可以在display:table之间添加一段代码:
									<display:setProperty value="list"/>
									3. 导出数据
									在display:table中添加export="true",看看会出现什么!Display Tag默认会提供三种数据导出方式:CSV、Excel、XML 。
									另外Display Tag还可以导出为PDF格式,在http://prdownloads.sourceforge.net/itext/下载一个辅助包iText.jar,copy到lib目录下,然后在display:table之间添加一段代码:
									<display:setProperty value="true"/>,大功告成。
									4. Display Tag的属性设置
									前面所说的display:setProperty 是一种改变Display Tag属性的方法,但是在每个jsp中都要写太麻烦了。
									Display Tag中设置了很多默认的属性,它有一个专门的属性文件,是在它的jar包中的displaytag/properties/TableTag.properties
									想要改变它的默认属性,我们可以在WEB-INF/classes下新建一个文件displaytag.properties,仿照TableTag.properties中属性的格式设置需要修改的属性。
									TableTag.properties中的# messages中设置的是显示在页面上的提示信息。默认是英文的,我们可以把它改为中文的。不过这里只能使用unicode,就是说中文字符必须转换为unicode码,这个可以使用jdk自带的native2ascii.exe进行转换。
5. displaytag中decorator的使用原理
									decorator有两种,一种是用在displaytag:table中,一种是用在displaytag:column中,前者对整个表中相应属性有效,后者对单个列有效。所以,在前者中,你一定要在响应的decorator类中写上get方法,命名方式为get+beanProperty(bean属性),前者需继承TableDecorator类,后者只需实现ColumnDecorator类的decorate方法即可(return一个String)。
									关于decorator的原理,是这样的,当使用decorator属性时,数据先从bean中被取出,然后被传入进decorator,前者是在decorator类中调用getCurrentRowObject方法得到当前bean,继而再调用bean的get方法将属性取出
									(
									public String getDate()
									{
									return this.dateFormat.format(((ListObject) this.getCurrentRowObject()).getDate());
									}
									),
									后者是数据直接被传进其decorate方法.
									当数据被处理完成后,再返回到页面显示,所以说,decorator类其实是将原有属性的value进行包装后输出的包装器,它的英文意思为油漆工,也就是这个意思,这也是一种基本设计模式
									但是如果碰到你想要输出一个checkbox或者radio时,你可以覆盖掉以前的输入,直接输出自己想要的东西,像这样 :return ""; 那么你就把输入进包装器的数据覆盖成checkbox输出了。
6. displaytag的翻页机制
									这可能是displaytag的局限性了,它的翻页机制是这样的:
									如果一个list中有10000个bean,按照它的机制,如果是第一页(每页n条),它会把前n条数据取出来,然后再把剩余的10000-n条删除,当你点击页面“2”的时候,它再从后台绕一圈,把第二页的数据,也就是把第n+1-2n条记录取出来,把剩余的删除。这样,它实现了翻页,又防止了内存占用过大。
									但是,不管怎么说,它还是有一个取出所有条数的动作的,在极大数据量的情况下,有可能造成内存溢出。
8. displaytag中decorator一例
									后台部分:
									=======================================================
									import org.displaytag.decorator.TableDecorator;
									import java.util.HashMap;
									import java.text.SimpleDateFormat;
									import java.util.Date;
									public class BulletinListDecorator extends TableDecorator{
									private String bulletinId = null;
									private String title = null;
									private SimpleDateFormat sdf = null;
									private int i = 0;
									public BulletinListDecorator() {
									sdf = new SimpleDateFormat("yy-MM-dd");
									}
									public String getBulletinId(){
									i+=1;
									return ""+i;
									}
									public String getTitle(){
									bulletinId = (String)((HashMap) this.getCurrentRowObject()).get("bulletinId");
									title = (String)((HashMap) this.getCurrentRowObject()).get("title");
									return ""+title+"";
									}
									public String getLastPubD(){
									return sdf.format((Date)((HashMap) this.getCurrentRowObject()).get("lastPubD"));
									}
									}
									====================================================
									
									前台部分:
									====================================================
									<display:tabledecorator="com.ztesoft.ds.application.web.bulletin.decorator.BulletinListDecorator">
									<display:column property="bulletinId" titleKey="titlePage.seq" align="center"headerClass="ValueTd"/>
									<display:column property="title" titleKey="titlePage.title" align="center" headerClass="ValueTd"/>
									<display:column property="lastPubD" titleKey="titlePage.time" align="center"headerClass="ValueTd"/>
									<display:column property="className" titleKey="titlePage.class" align="center"headerClass="ValueTd"/>
									<display:column property="clickNo" titleKey="titlePage.clickNo" align="center"headerClass="ValueTd"/>
									</display:table>
其中bulletinList