分页

来源:互联网 发布:oss js 上传文件 跨域 编辑:程序博客网 时间:2024/05/01 22:48

package com.li.utils;

public class PageIndex {
 private long startindex;
 private long endindex;
 
 public PageIndex(long startindex, long endindex) {
  this.startindex = startindex;
  this.endindex = endindex;
 }
 public long getStartindex() {
  return startindex;
 }
 public void setStartindex(long startindex) {
  this.startindex = startindex;
 }
 public long getEndindex() {
  return endindex;
 }
 public void setEndindex(long endindex) {
  this.endindex = endindex;
 }
 
 public static PageIndex getPageIndex(long viewpagecount, int currentPage, long totalpage){
   long startpage = currentPage-(viewpagecount%2==0? viewpagecount/2-1 : viewpagecount/2);
   long endpage = currentPage+viewpagecount/2;
   if(startpage<1){
    startpage = 1;
    if(totalpage>=viewpagecount) endpage = viewpagecount;
    else endpage = totalpage;
   }
   if(endpage>totalpage){
    endpage = totalpage;
    if((endpage-viewpagecount)>0) startpage = endpage-viewpagecount+1;
    else startpage = 1;
   }
   return new PageIndex(startpage, endpage);  
 }
}

 

 

package com.li.bean.service.base;

import java.util.List;

import com.li.utils.PageIndex;

public class Paging<T> {
 private List<T> pageList;
 private long totalRecords;
 private long totalPage;
 
 private int currentPage = 1;
 private int maxResult = 12;

 private PageIndex pageIndex;

 // 分页条上现实的页数
 private int countPage= 5;
 

 

 public void setPageModel(PageModel<T> pageModel) {
  if (pageModel != null) {

   this.pageList = pageModel.getPageList();
   
   this.totalRecords = pageModel.getTotalrecordNo();
   
   this.totalPage = (totalRecords % maxResult == 0) ? totalRecords
     / maxResult : (totalRecords / maxResult) + 1;
   
   this.pageIndex = PageIndex.getPageIndex(countPage,this.currentPage, totalPage);
     
  }
 }

 public long getTotalPage() {
  return totalPage;
 }

 public List<T> getPageList() {
  return pageList;

 }

 public int getCurrentPage() {
  return currentPage < 1 ? 1 : this.currentPage;
 }

 public void setCurrentPage(int currentPage) {
  if (currentPage > 0) {
   this.currentPage = currentPage;

  } else {
   currentPage = 1;
  }

 }

 public int getMaxResult() {
  return maxResult;
 }

 public long getFirstIndex() {
  return (this.currentPage - 1) * this.maxResult;
 }

 

 public PageIndex getPageIndex() {
  return pageIndex;
 }

 public Paging() {
  System.out.println("新建个Paging 对象");
 }

}

 

 @Override
 public String execute() throws Exception {

      PageModel<ProductType> pageModel = productTypeService.getPageData(
      ProductType.class, (int) paging.getFirstIndex(), paging  .getMaxResult());
       this.paging.setPageModel(pageModel);
      HttpServletRequest request =ServletActionContext.getRequest();
      request.setAttribute("paging", paging);
  return "success";
 
 }

 

 

<c:forEach begin="${paging.pageIndex.startindex}"
      end="${paging.pageIndex.endindex}" var="wp">
      <c:if test="${paging.currentPage==wp}">
       <b>第${wp}页</b>
      </c:if>
      <c:if test="${paging.currentPage!=wp}">
       <a href="product_typelist.action?paging.currentPage=${wp}"
        class="a03">第${wp}页</a>
      </c:if>

     </c:forEach>