java分页

些篇也是以前写在QQ空间上,没什么人关注,还是转到这里跟大家分享

 

搞了两三天,找了好几个例子,最后自己理解,封装一个分页的好例子,拿出来分享
主要是这个类
package com.haige.chinaxyrz.util.pagination;
import java.util.ArrayList;
import java.util.List;
public class ShowByPageImpl {

private int currentPage=1;//当前页
private int totalPages=0;//总页数

private int showPages=10;//分页要显示的页数

private int pageRecorders=2;//每页记录

private int totalRows=0;//总行数

private int pageStartRow=0;//每页的起始行

private int pageEndRow=0;//每页显示的终止数

private boolean hasNextPage=false;//没有下一页

private boolean hasPreviousPage=false;//没有上一页

private List list;//传过一个list,就可以对list进行分页
private String url;//action跳转页面
public ShowByPageImpl(List list,String url) {
  this.list = list;
  this.url=url;
  totalRows = list.size();
  hasPreviousPage = false;
  currentPage = 1;
  if (totalRows % pageRecorders == 0) {
   totalPages = totalRows / pageRecorders;
  } else {
   totalPages = totalRows / pageRecorders + 1;
  }
  if (currentPage >= totalPages) {
   hasNextPage = false;
  } else {
   hasNextPage = true;
  }
  if (totalRows < pageRecorders) {
   this.pageStartRow = 0;
   this.pageEndRow = totalRows;
  } else {
   this.pageStartRow = 0;
   this.pageEndRow = pageRecorders;
  }
}
public int getCurrentPage() {
  return currentPage;
}
public void setCurrentPage(int currentPage) {
  this.currentPage = currentPage;
}
public int getTotalPages() {
  return totalPages;
}
public void setTotalPages(int totalPages) {
  this.totalPages = totalPages;
}
public int getPageRecorders() {
  return pageRecorders;
}
public void setPageRecorders(int pageRecorders) {
  this.pageRecorders = pageRecorders;
}
public int getTotalRows() {
  return totalRows;
}
public void setTotalRows(int totalRows) {
  this.totalRows = totalRows;
}
public int getPageStartRow() {
  return pageStartRow;
}
public void setPageStartRow(int pageStartRow) {
  this.pageStartRow = pageStartRow;
}
public int getPageEndRow() {
  return pageEndRow;
}
public void setPageEndRow(int pageEndRow) {
  this.pageEndRow = pageEndRow;
}
public boolean isHasNextPage() {
  return hasNextPage;
}
public void setHasNextPage(boolean hasNextPage) {
  this.hasNextPage = hasNextPage;
}
public boolean isHasPreviousPage() {
  return hasPreviousPage;
}
public void setHasPreviousPage(boolean hasPreviousPage) {
  this.hasPreviousPage = hasPreviousPage;
}
public List getList() {
  return list;
}
public void setList(List list) {
  this.list = list;
}

//首页
public void getFirstPage() {
  currentPage = 1;
  hasPreviousPage = false;
  if (currentPage >= totalPages) {
   hasNextPage = false;
  } else {
   hasNextPage = true;
  }
}

//下一页
public void getNextPage() {
  currentPage = currentPage + 1;
  if ((currentPage - 1) > 0) {
   hasPreviousPage = true;
  } else {
   hasPreviousPage = false;
  }
  if (currentPage >= totalPages) {
   hasNextPage = false;
  } else {
   hasNextPage = true;
  }
}

//上一页
public void getPreviousPage() {
  currentPage = currentPage - 1;
  if (currentPage == 0) {
   currentPage = 1;
  }
  if (currentPage >= totalPages) {
   hasNextPage = false;
  } else {
   hasNextPage = true;
  }
  if ((currentPage - 1) > 0) {
   hasPreviousPage = true;
  } else {
   hasPreviousPage = false;
  }
}

//最后一页
public void getLastPage() {
  currentPage = totalPages;
  if (currentPage == 0) {
   currentPage = 1;
  }
  hasNextPage = false;
  if ((currentPage - 1) > 0) {
   hasPreviousPage = true;
  } else {
   hasPreviousPage = false;
  }
}

//获取选择页
public void getSelectPage(int selectPage){
  currentPage = selectPage;
  if(currentPage>=totalPages)
   hasNextPage=false;
  else
   hasNextPage=true;
  if((currentPage-1)>0)
   hasPreviousPage=true;
  else
   hasPreviousPage=false;
}

//获取当前列表
public List getPageList() {
  if (currentPage * pageRecorders < totalRows) {
   pageEndRow = currentPage * pageRecorders;
   pageStartRow = pageEndRow - pageRecorders;
  } else {
   pageEndRow = totalRows;
   pageStartRow = pageRecorders * (totalPages - 1);
  }
  List templist = new ArrayList();
  for (int j=0,i = pageStartRow; i < pageEndRow;j++,i++)
   templist.add(j, list.get(i));
  return templist;
}

//控制页面跳转
public void controlPage(String action){
  if(action.equals("firstPage"))
   this.getFirstPage();
  else if(action.equals("nextPage"))
   this.getNextPage();
  else if(action.equals("previousPage"))
   this.getPreviousPage();
  else if(action.equals("lastPage"))
   this.getLastPage();
  else
   this.getSelectPage(Integer.valueOf(action));
}


//返回分页字段,这部分是自己写的,表达的也不是很清楚,不过多看几篇应该可以理解
public StringBuffer getPaginalString(){
  StringBuffer paginalString=new StringBuffer();
  paginalString.append("共"+String.valueOf(totalRows)+"条记录 ").append("共"+String.valueOf(totalPages)+"页 ").append("当前第"+Integer.valueOf(currentPage)+"页 ");
  if(hasPreviousPage)//如果有前页
   paginalString.append("<a href='"+url+"firstPage' style='text-decoration:none'>[首页]</a>").append("<a href='"+url+"previousPage' style='text-decoration:none'>[上一页]</a>");
  if(totalPages<=showPages)//实际总页数<=想显示的页数
      for(int i=1;i<=totalPages;i++){
       paginalString.append("&nbsp&nbsp<a href='peopleSearch.do?action="+String.valueOf(i)+"'");
       if(i!=currentPage)//如果是当前页
        paginalString.append("style='text-decoration:none'>["+i+"]</a>");
       else//如果不是当前页
        paginalString.append("style='font-size:18px;color:#FF0000;text-decoration:none'>"+i+"</a>");
      }
  else if(totalPages>showPages){//实际总页数大于页面总页数
   if(currentPage>(showPages+1)/2){//当前页大于页面显示的页数的一半,如123456789,则当前页为6789
    //下面主要判断,如果当前页大于页面显示的页数的一半时,考虑当前页是否在页面显示的中间
    //如123456789总共页数为15当你点第8页时,分页应该4 5 6 7 [8] 9 10 11 12 如果点了12页则分页应为7 8 9 10 11 [12] 13 14 15因为后面没有了所以第12没在中间
    if(currentPage<=totalPages-showPages/2)//当前页大于页面显示的页数的一半,且最后页数够,显示的分页
        for(int i=1;i<=showPages;i++){
              paginalString.append("&nbsp&nbsp<a href='"+url+String.valueOf(currentPage+i-(showPages+1)/2)+"'");
        if((currentPage+i-(showPages+1)/2)!=currentPage)
            paginalString.append("style='text-decoration:none'>["+(currentPage+i-(showPages+1)/2)+"]</a>");
        else
            paginalString.append("style='font-size:18px;color:#FF0000;text-decoration:none'>"+(currentPage+i-(showPages+1)/2)+"</a>");              
        }
       else if(currentPage>totalPages-showPages/2)//当前页大于页面显示的页数的一半,最后页数不够,显示的分页
              for(int i=1;i<=showPages;i++){
               paginalString.append("&nbsp&nbsp<a href='"+url+String.valueOf(currentPage-(showPages-(totalPages-currentPage))+i)+"'");//currentPage-(showPages-(totalPages-currentPage))+i
         if((currentPage-(showPages-(totalPages-currentPage))+i)!=currentPage)
          paginalString.append("style='text-decoration:none'>["+(currentPage-(showPages-(totalPages-currentPage))+i)+"]</a>");
         else
          paginalString.append("style='font-size:18px;color:#FF0000;text-decoration:none'>"+(currentPage-(showPages-(totalPages-currentPage))+i)+"</a>");
              }
   }
   else if(currentPage<=(showPages+1)/2)//当前页小于页面显示的页数的一半,如123456789,则当前页为12345
       for(int i=1;i<=showPages;i++){
        paginalString.append("&nbsp&nbsp<a href='"+url+String.valueOf(i)+"'");
        if(i!=currentPage)
         paginalString.append("style='text-decoration:none'>["+i+"]</a>");
        else
         paginalString.append("style='font-size:18px;color:#FF0000;text-decoration:none'>"+i+"</a>");
       }
  }
  if(hasNextPage) //如果有后页
   paginalString.append("&nbsp&nbsp<a href='"+url+"nextPage' style='text-decoration:none'>[下一页]</a>").append("<a href='"+url+"lastPage' style='text-decoration:none'>[末页]</a>");
  return paginalString;
}
}
action中调用上面这个类
package com.haige.chinaxyrz.struts.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.haige.chinaxyrz.business.inter.IPeopleBusiness;
import com.haige.chinaxyrz.pojo.Peopleinfo;
import com.haige.chinaxyrz.util.pagination.ShowByPageImpl;

public class PeopleSearchAction extends Action {
  List<Peopleinfo> searchResult;
  List<Peopleinfo> searchAllResult;
     IPeopleBusiness ipb;
  ShowByPageImpl sbpi=null;
    
public void setIpb(IPeopleBusiness ipb) {
  this.ipb = ipb;
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  String searchContent=request.getParameter("searchContent");
  String action=request.getParameter("action");
//这样子在action 就变的很简单了,只要两个判断就行了
  if(action.equals("initPage")){//如果提交的是初始化,就去访问一个结果保存在lis类型
    searchAllResult=ipb.peopleSearch(searchContent.trim());//trim去掉前后空格
    sbpi=new ShowByPageImpl(searchAllResult,"peopleSearch.do?action=");
  }
  else{
   sbpi.controlPage(action);//控制页面跳转
  }

  searchResult=sbpi.getPageList();
  String paginalString=sbpi.getPaginalString().toString();
  request.setAttribute("paginalString", paginalString);
  request.setAttribute("searchResult",searchResult);
  return new ActionForward("/jsp/peopleList.jsp");
}
}
页面(页面变的很简单)
  <body>
    <logic:iterate id="aBean" name="searchResult" >
       <!--<bean:write name="aBean" property="peopleinfoName"/>-->
       ${aBean.peopleinfoName}
      </logic:iterate>
${paginalString}
  </body>
效果








 

你可能感兴趣的:(java,DAO,Hibernate,浏览器,struts)