您的位置:首页 > Web前端 > JavaScript

Displaytag 1.1.1分页功能基本用法

2008-09-05 15:20 141 查看
1、要有一个表示页面的类,通常采用PaginationSupport

 

java代码如下:

 

import java.util.List;
/**
 * hibernate分页 Page对象
 * @author lele
 *
 */
public class PaginationSupport
{

         public final static int PAGESIZE = 30;

         private int    pageSize = PAGESIZE;

         private List   items;//分页查询结果

         private int    totalCount;

         private int[]   indexes  = new int[0]; //有多少页以及每页的记录数

         private int    startIndex = 0;//开始index

 

         public PaginationSupport(List items, int totalCount)   {
              setPageSize(PAGESIZE);
              setTotalCount(totalCount);
              setItems(items);
              setStartIndex(0);
         }

         public PaginationSupport(List items, int totalCount, int startIndex) {
              setPageSize(PAGESIZE);
              setTotalCount(totalCount);
              setItems(items);
              setStartIndex(startIndex);
         }

         public PaginationSupport(List items, int totalCount, int pageSize, int startIndex) {
              setPageSize(pageSize);
              setTotalCount(totalCount);
              setItems(items);
              setStartIndex(startIndex);
         }

         public List getItems()  {
              return items;
         }

         public void setItems(List items)  {
              this.items = items;
         }

         public int getPageSize() {
              return pageSize;
         }

         public void setPageSize(int pageSize)  {
              this.pageSize = pageSize;
         }

         public int getTotalCount()  {
              return totalCount;
         }

         public void setTotalCount(int totalCount)  {
              if (totalCount > 0)  {
                   this.totalCount = totalCount;
                   int count = totalCount / pageSize;
                   if (totalCount % pageSize > 0)  {
                            count++;
                   }
                   indexes = new int[count];
                   for (int i = 0; i < count; i++)  {
                            indexes[i] = pageSize * i;
                   }
              } else {
                   this.totalCount = 0;
              }
         }

         public int[] getIndexes()  {
              return indexes;
         }

         public void setIndexes(int[] indexes) {
              this.indexes = indexes;
         }

         public int getStartIndex() {
              return startIndex;
         }

         public void setStartIndex(int startIndex) {
              if (totalCount <= 0) {
                   this.startIndex = 0;
              } else if (startIndex >= totalCount) {
                   this.startIndex = indexes[indexes.length - 1];
              } else if (startIndex < 0) {
                   this.startIndex = 0;
              } else {
                   this.startIndex = indexes[startIndex / pageSize];
              }
         }

         public int getNextIndex()  {
              int nextIndex = getStartIndex() + pageSize;
              if (nextIndex >= totalCount)  {
                   return getStartIndex();
              } else {
                   return nextIndex;
              }
         }

         public int getPreviousIndex() {
                  int previousIndex = getStartIndex() - pageSize;
                  if (previousIndex < 0) {
                       return 0;
                  } else  {
                       return previousIndex;
                  }
         }

}

 

在Action中提取分页的信息:

 

import org.displaytag.tags.TableTagParameters;
import org.displaytag.util.ParamEncoder;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

 

//从JSP内存中找到infoList这个标签,获取当前页面
String pageNumName = new ParamEncoder("infoList").encodeParameterName(TableTagParameters.PARAMETER_PAGE);

//如果当前页面为空,则设置页面为1
int pageIndex = GenericValidator.isBlankOrNull(request.getParameter(pageNumName))?1:(Integer.parseInt(request.getParameter(pageNumName)));

 

//PaginateionSupport是一个公用的类
PaginationSupport paginationSupport = null;

 

//运用hibernate3提供的DetachedCriteria功能实现动态查询

//TBulletin.class是要查找的表
DetachedCriteria detachedCriteria=DetachedCriteria.forClass(TBulletin.class);

//添加一些限制条件

detachedCriteria.add(Restrictions.eq("status", status));
detachedCriteria.add(Restrictions.eq("buildingId", user.getBuildingId()));
detachedCriteria.addOrder(Order.desc("openTime"));

//TBulleltinDAO中写一个findPageByCriteria方法,传入每页显示的条数和开始显示的位置
paginationSupport = tBulletinDAO.findPageByCriteria(detachedCriteria, Constanst.PAGE_SIZE, (pageIndex-1)*Constanst.PAGE_SIZE);

request.setAttribute("paginationSupport", paginationSupport);

3、TBulletinDAO中findPageByCriteria方法代码如下:

public PaginationSupport findPageByCriteria( final DetachedCriteria detachedCriteria,

                        final int pageSize,   final int startIndex) {
      return (PaginationSupport) getHibernateTemplate().execute(
               new HibernateCallback() {
                       public Object doInHibernate(Session session)  throws HibernateException {
                             Criteria criteria = detachedCriteria .getExecutableCriteria(session);
                             int totalCount = ((Integer) criteria.setProjection( Projections.rowCount()).uniqueResult()).intValue();  
                             criteria.setProjection(null);
                             detachedCriteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
                             List items = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();
                             PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);
                               return ps;
                      }
             }, true);
}

只要返回表示页面的类,而从数据库读取的数据集合不用显示传回,只要在jsp页面中设置proerty对应命名相同的属性即可。

 

4、在JSP页面中的代码如下:

 

#id是在Action中要获取的页面标识

#size是总的满足查询条件的条目数

#pagesize是总的页面数

#requestURI是要读取页面数据的Action

<displaytag:table name="paginationSupport.items" id="infoList"  size="paginationSupport.totalCount" pagesize="${paginationSupport.pageSize}"    partialList="true"
 class="mars" export="false" requestURI="InfoListPublish.do">

    /*media设置可以显示html代码,在title中插入html代码并在页面上显示
    <displaytag:column media="html" title="<input type='checkbox' name='chkall' onclick='CheckAll(this.form)' />"> <input type="checkbox" name="nodeId" value="${infoList.bulletinId}"></displaytag:column>
    <displaytag:column property="serviceType" title="信息类型"  decorator="com.fsti.ibuilding.web.commons.decorator.BulletinServiceType"/>
    <displaytag:column title="状态" ><img src="../images/table/icon_published.gif" border="0"/></displaytag:column>
    <displaytag:column property="bulletinTitle" title="标题" />

   /*decorator对数据格式进行转换*/
    <displaytag:column property="openTime" title="创建时间" decorator="com.fsti.ibuilding.web.commons.decorator.TimeSub16"/>
    <displaytag:column property="expTime" title="截止时间" decorator="com.fsti.ibuilding.web.commons.decorator.TimeSub16"/>
    <displaytag:column title="编辑">
          <a href="infoEdit.do?editType=publish&id=${infoList.bulletinId}">
            <img src="../images/table/edit.gif" width="18" height="18" alt="" onclick="" />
          </a>
     </displaytag:column>
 </displaytag:table>

 

5、displaytag.properties 配置文件

export.decorated=true

paging.banner.group_size=5
paging.banner.placement=bottom

 

#css中对table的一些属性的设置
css.tr.even=even
css.tr.odd=odd
css.th.sorted=sorted
css.th.ascending=order1
css.th.descending=order2
css.table=
css.th.sortable=sortable

 

# factory classes for extensions
factory.requestHelper=org.displaytag.util.DefaultRequestHelperFactory

 

# factory class for decorators
factory.decorator=org.displaytag.decorator.DefaultDecoratorFactory

 

# locale provider (Jstl provider by default)
locale.provider=org.displaytag.localization.I18nJstlAdapter

 

# messages

#{0}当前页面 {1}首页 {2}上一页 {3}下一页 {4}末页

basic.msg.empty_list=未找到任何记录!
basic.msg.empty_list_row=<tr class="empty"><td colspan="{0}">未找到任何记录!</td></tr>

paging.banner.item_name=item
paging.banner.items_name=items

paging.banner.no_items_found=<span class="pagebanner">共找到 <b>0</b> 条记录</span>
paging.banner.one_item_found=<span class="pagebanner">共找到 <b>1</b> 条记录</span>
paging.banner.all_items_found=<span class="pagebanner">共找到 <b>{0}</b> 条记录, 当前显示全部</span>
paging.banner.some_items_found=<span class="pagebanner">共找到 <b>{0}</b> 条记录, 当前显示 <b>{2}</b> 到 <b>{3}</b></span>

paging.banner.full=<span class="pagelinks">[<a href="{1}">首页</a>/<a href="{2}">上一页</a>] {0} [<a href="{3}">下一页</a>/<a href="{4}">末页</a>]</span>
paging.banner.first=<span class="pagelinks">[首页/上一页] {0} [<a href="{3}">下一页</a>/<a href="{4}">末页</a>]</span>
paging.banner.last=<span class="pagelinks">[<a href="{1}">首页</a>/<a href="{2}">上一页</a>] {0} [下一页/末页]</span>
paging.banner.onepage=<span class="pagelinks"> <b>{0}</b></span>

paging.banner.page.selected=<strong>{0}</strong>
paging.banner.page.link=<a href="{1}" title="Go to page {0}">{0}</a>
paging.banner.page.separator=, /

 

# external sort and pagination
pagination.sort.param=sort
pagination.sortdirection.param=dir
pagination.pagenumber.param=page
pagination.searchid.param=searchid
pagination.sort.asc.value=asc
pagination.sort.desc.value=desc
pagination.sort.skippagenumber=true

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息