标签实现分页

来源:互联网 发布:阿里云幕布拍照 编辑:程序博客网 时间:2024/05/22 16:57

1       基于DisplayTag的分页方案

DisplayTag说明<o:p></o:p>

Display Tag Lib是一个标签库,用来处理jsp网页上的Table,功能非常强,可以对的Table进行分页、数据导出、分组、对列排序等等。具体的说明请参考Display Tag的官方网站http://displaytag.sourceforge.net

       DisplayTag为一个开源的标签库,使用DisplayTag必须从网站下载DisplayTagjar包,并将jar引用到用户所用的工程中。

       同时,DisplayTag.jar依赖Apache项目的支持,所以在使用同时,必须下载Apache相关的jar包。

 <o:p></o:p>

配置类库标签<o:p></o:p>

DisplayTag使用了两个标签,displaytag-1.1.tldfmt.tld。使用了servlet2.4以前的版本,需要在web.xml中配置。

<taglib><o:p></o:p>

    <taglib-uri>http://displaytag.sourceforge.net/</taglib-uri><o:p></o:p>

    <taglib-location>/WEB-INF/displaytag-11.tld</taglib-location><o:p></o:p>

  </taglib><o:p></o:p>

  <taglib><o:p></o:p>

      <taglib-uri>/WEB-INF/fmt</taglib-uri><o:p></o:p>

      <taglib-location>/WEB-INF/fmt.tld</taglib-location><o:p></o:p>

  </taglib><o:p></o:p>

两个标签中,displaytag-1.1.tldDisplayTag的官方标签,另外fmt.tld的标签作用是为了显示中文而设定的。

 <o:p></o:p>

构造页面显示的List<o:p></o:p>

DisplayTag能够在requestsession范围内获得相关的List。所有的数据必须从List中获得。

 <o:p></o:p>

1.单表查询结果集<o:p></o:p>

List userList=getHibernateTemplate().find(“from user”);<o:p></o:p>

Request.setAttribute(“userList”, userList);<o:p></o:p>

2.多表查询结果集<o:p></o:p>

List userCustomerList=getHibernateTemplate().find(“select new MyObject(user.Name,user.No…..) from user,customer”);<o:p></o:p>

Request.setAttribute(“userCustomerList”,” userCustomerList”);<o:p></o:p>

要先定义好MyObject这个class,并且有new MyObject(….)构造函数<o:p></o:p>

3.以上两种方法每次在翻页时,都是对所有查询结果进行一次查询,如果想实现每次只查询10条数据,则需要自己构造sql执行。DisplayTag提供了一些变量.<o:p></o:p>

String strPage = request.getParameter("page");

String strDir = request.getParameter("dir");

String strSort = request.getParameter("sort");

Actionrequest里面获得这三个参数,都是DisplayTag自动生成的,用户无需管理这三个参数。

 <o:p></o:p>

DisplayTag页面基本组成<o:p></o:p>

1.     要引入标签<o:p></o:p>

<%@ taglib uri="/WEB-INF/fmt.tld"             prefix="fmt" %>

<%@ taglib uri="http://displaytag.sf.net/el"        prefix="display" %>

2.     在页面上使用displayTag标签<o:p></o:p>

1) 最简单的情况<o:p></o:p>

<display:table name="test" /><o:p></o:p>

标签遍历List里的每一个对象,并将对象里的所有属性显示出来。一般用于开发的时候检查对象数据的完整性。<o:p></o:p>

2) 使用<display:column/>标签的情况<o:p></o:p>

  <display:table name="test"><o:p></o:p>

  <display:column property="id" title="ID" /><o:p></o:p>

  <display:column property="name" /><o:p></o:p>

  <display:column property="email" /><o:p></o:p>

  <display:column property="status" /><o:p></o:p>

  <display:column property="description" title="Comments"/><o:p></o:p>

  </display:table><o:p></o:p>

  property对应List里对象的属性(用getXXX()方法取得),title则对应表格表头里的列名。定义列有两种方式:<o:p></o:p>

  A<display:column property="email" /><o:p></o:p>

  使用<display:column/>标签里的property属性来定义<o:p></o:p>

  B<display:column title="email">email@it.com</display:column><o:p></o:p>

  在<display:column/>标签体里增加内容,可以是常量,也可以用其他标签等等<o:p></o:p>

两种方式比较,用property属性来定义更加快速和利于排序<o:p></o:p>

3) 表格显示样式的定义<o:p></o:p>

A、        <display:table/><display:column/>标签里指定标准的html属性<o:p></o:p>

<display:table name="test" style=”……”><o:p></o:p>

<display:column property="id" title="ID" style=”……”/><o:p></o:p>

</display><o:p></o:p>

 <o:p></o:p>

  B、修改样式表<o:p></o:p>

  <display:table name="test" class="mars"><o:p></o:p>

  <display:column property="id" title="ID" class="idcol"/><o:p></o:p>

  <display:column property="name" /><o:p></o:p>

  <display:column property="email" /><o:p></o:p>

  <display:column property="status" class="tableCellError" /><o:p></o:p>

  <display:column property="description" title="Comments"/><o:p></o:p>

  </display:table><o:p></o:p>

  <o:p></o:p>

通过class属性来指定所要应用的样式(自己定义的样式)。也还可以在其默认样式表里(./css/screen.css)直接修改<o:p></o:p>

4) 标签取得数据的数据源<o:p></o:p>

  有四种范围<o:p></o:p>

  pageScope<o:p></o:p>

  requestScope (默认) <display:table name="test2" ><o:p></o:p>

  sessionScope <display:table name="sessionScope.holder.list" > 注意,这里要指定范围,非默认<o:p></o:p>

  applicationScope<o:p></o:p>

5) 通过增加id属性创建隐含的对象<o:p></o:p>

<display:table id="row" name="mylist"><o:p></o:p>

  <display:column title="row number" ><o:p></o:p>

  <c:out value="${row_rowNum}"/><o:p></o:p>

  </display:column><o:p></o:p>

  <display:column title="name" ><o:p></o:p>

  <a href=”#” onclick=”view(‘${row.id}’)”>${row.first_name}-${row.last_name}</a><o:p></o:p>

 <o:p></o:p>

    </display:column><o:p></o:p>

  </display:table><o:p></o:p>

注意到在<display:table/>里增加了id属性,这时就在page context里创建了一个隐含对象,指向List里的当前对象; 同时还创建了一个id_rowNum对象,它仅仅代表当前行的行数。<o:p></o:p>

6) 显示部分数据<o:p></o:p>

显示开始五条数据:通过设定length属性<o:p></o:p>

  <display:table name="test" length="5"><o:p></o:p>

  <display:column property="id" title="ID" /><o:p></o:p>

  <display:column property="email" /><o:p></o:p>

  <display:column property="status" /><o:p></o:p>

  </display:table><o:p></o:p>

  显示第三到第八条数据:通过设定offsetlength属性<o:p></o:p>

  <display:table name="test" offset="3" length="5"><o:p></o:p>

  <display:column property="id"

原创粉丝点击