Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Servlet/JSP/JSF/JavaFX Script  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
reply to postflat modethreaded modego to previous topicgo to next topicgo to back
作者 Display Tag使用小记
bilang





发贴: 14
于 2005-10-23 17:42 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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 name="test" />
display tag会自动生成一个table

如果list是从控制层抛出来的,name可使用EL表达式表示
<display:table name="${test}" />

这是最简单的display tag的使用,我们可以给它加上样式等,也可以定义显示的列,下面的table显示复杂一些
<display:table name="test" styleClass="list" cellspacing="0" cellpadding="0">
<display:column property="id" title="ID" class="idcol"/>
<display:column property="name" />
<display:column property="email" />
<display:column property="description" title="Comments"/>
</display:table>

如果想要给它加个链接也很简单,下面的代码给name加了连接,并附带id参数,email也自动连接到mailtoKissesXX
<display:table name="test" styleClass="list" cellspacing="0" cellpadding="0">
<display:column property="id" title="ID" class="idcol"/>
<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 name="test" pagesize="10"/>

2. 对列排序
display tag可对列进行排序,就是点击列名,对该列的数据进行排序。你只需对想要排序的列添加 sort="true" 就OK,如下面的代码可对前三列进行排序。在display:table中添加defaultsort="列数",可默认对指定的列排序。
<display:table name="test" styleClass="list" cellspacing="0" cellpadding="0" defaultsort="1">
<display:column property="id" title="ID" class="idcol" 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 name="sort.amount" 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 name="export.pdf" 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条记录取出来,把剩余的删除。这样,它实现了翻页,又防止了内存占用过大。

但是,不管怎么说,它还是有一个取出所有条数的动作的,在极大数据量的情况下,有可能造成内存溢出。




话题树型展开
人气 标题 作者 字数 发贴时间
1770 Display Tag使用小记 bilang 4448 2005-10-23 17:42

reply to postflat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923