JSP通用分页程序(测试版)

转载 2007年10月12日 13:00:00

最终效果图:

1.分页类:

---------------------------------------------------------------------------------------------

package cn.lingqi.tool;

import java.io.Serializable;
import java.util.ArrayList;

public class PageFunction implements Serializable{
 private int page; //当前页码
 private int totalCount; //总记录数
 private int pageSie = 10; //每页条数
 private int barSize = 10; //每bar显示页数
 
 public PageFunction(){}
 public PageFunction(int page, int totalCount, int pageSie, int barSize) {
  super();
  this.page = page;
  this.totalCount = totalCount;
  this.pageSie = pageSie;
  this.barSize = barSize;
 }


 public int getBarSize() {
  return barSize;
 }


 public void setBarSize(int barSize) {
  this.barSize = barSize;
 }


 public int getPage() {
  return page;
 }


 public void setPage(int page) {
  this.page = page;
 }


 public int getPageSie() {
  return pageSie;
 }


 public void setPageSie(int pageSie) {
  this.pageSie = pageSie;
 }


 public int getTotalCount() {
  return totalCount;
 }


 public void setTotalCount(int totalCount) {
  this.totalCount = totalCount;
 }


 public ArrayList paging(){
  int page = this.page;
  if (page == 0){
   page =1;
  }
  if (page == (getpageCount()+1)){
   page = getpageCount();
  }
  int pageSize = this.pageSie;
  int total = this.totalCount;
  int barSize = this.barSize;
  ArrayList mypa = new ArrayList();
  int pageCount = (total-1)/pageSize+1;//总页数
  int barPageCount = (pageCount-1)/barSize+1; //bar的总页数
  int currentBar = (page-1)/barSize+1; //当前bar号
  if (page == pageCount ||currentBar==barPageCount){//判断当前bar是否最后页 或 当前页是否最后页
   int xx = pageCount%barSize ; //最后页bar记录数
   int i = (page-1)/barSize; //获取前面N-1页bar数
   for (int j=0;j<xx;j++){
    mypa.add(barSize*i+(j+1)); //最后页bar的页码数
    //mypa[j]=j;
   }
  }else{
  int i = (page-1)/barSize; //获取前面N-1页bar数
  for (int ii=0;ii<barSize;ii++){
   mypa.add(barSize*i+(ii+1)); //最后页bar的页码数
  }
  }
  return mypa;
 }
 
 public int getBarCount(){
  int pageSize = this.pageSie;
  int total = this.totalCount;
  int pageCount = (total-1)/pageSize+1;//总页数
  int barPageCount = (pageCount-1)/barSize+1; //bar的总页数
  return barPageCount;
 }
 
 public int getpageCount(){
  int pageSize = this.pageSie;
  int total = this.totalCount;
  int pageCount = (total-1)/pageSize+1;//总页数
  return pageCount;
 }
 
 public int getCurrentBar(){ //获得当前页所在的bar号
  int page = this.page;
  int barSie = this.barSize;
  return (page-1)/barSize+1;
 }
}

2.实现分页的page.jsp:

---------------------------------------------------------------------------------------------


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<table width="100%">
  <tr>
    <td><div align="right">
    <logic:notEqual name="nowpage" value="1">
    <a href="./newsOption.do?method=findpage&page=1&pageType=first">最前页</a>
    </logic:notEqual>
    <logic:notEqual name="currentBar" value="1">
    <a href="./newsOption.do?method=findpage&currentBar=${currentBar}&barType=pre">上十页</a>
    </logic:notEqual>
    <logic:iterate id="mylist" name="pageList" scope="session">

 <a href="./newsOption.do?method=findpage&page=<bean:write name="mylist"/>"> <font color='<logic:equal name="nowpage" value="${mylist}">red</logic:equal>'><bean:write name="mylist"/></font></a>
 </logic:iterate>
 <logic:notEqual name="currentBar" value="${barcount}">
 <a href="./newsOption.do?method=findpage&currentBar=${currentBar}&barType=next">下十页</a>
 </logic:notEqual>
 <logic:notEqual name="nowpage" value="${pagecount}">
 <a href="./newsOption.do?method=findpage&page=${pagecount}&pageType=last">最后页</a>
 </logic:notEqual>
 </div></td>
  </tr>
</table>

 

3.第一次登陆调用的代码

---------------------------------------------------------------------------------------------------

PageFunction pf = new PageFunction();
   NewsFormDAO ndao = new NewsFormDAO();
   pf.setTotalCount(ndao.newsCount());
   pf.setPage(1);
   ArrayList listNews = (ArrayList) ndao.queryPage(1, pf.getPageSie());
   int currentBar = (pf.getPage()-1)/pf.getBarSize()+1;
   session.setAttribute("currentBar",currentBar); //当前bar号
   session.setAttribute("listNews", listNews);
   session.setAttribute("pageList", pf.paging()); //当前bar中的页号列表
   session.setAttribute("nowpage",1); //当前页号
   session.setAttribute("barcount",pf.getBarCount()); //bar总页数
   session.setAttribute("pagecount",pf.getpageCount()); //总页数

4.由page.jsp触发的代码

------------------------------------------------------------------------------------------------------

public ActionForward findPage(
   ActionMapping mapping,
   ActionForm form,
   HttpServletRequest request,
   HttpServletResponse response) {
  HttpSession session = request.getSession();
  PageFunction pf = new PageFunction();
  NewsFormDAO ndao = new NewsFormDAO();
  String currentBar = request.getParameter("currentBar");
  String barType = request.getParameter("barType");
  String pagetemp = request.getParameter("page");
  String pageType = request.getParameter("pageType");
  int  nowCurrentBar= 0 ;
  int page = 0;
  pf.setTotalCount(ndao.newsCount());
  if ("next".equals(barType)&&!"".equals(currentBar)){
   page = Integer.parseInt(currentBar)*pf.getBarSize()+1;
   nowCurrentBar = Integer.parseInt(currentBar)+1;
   if (nowCurrentBar == pf.getBarCount()){
    nowCurrentBar = pf.getBarCount();
   }
  }
  if ("pre".equals(barType)&&!"".equals(currentBar)){
   page = (Integer.parseInt(currentBar)-1)*pf.getBarSize();
   nowCurrentBar = Integer.parseInt(currentBar)-1;
   if (nowCurrentBar==1){
    nowCurrentBar=1;
   }
  }
  if (!"".equals(pagetemp)&&pagetemp!=null){
   page = Integer.parseInt(pagetemp);
  }
  if ("first".equals(pageType)){
   nowCurrentBar =1;
  }
  if ("last".equals(pageType)){
   nowCurrentBar =pf.getBarCount();
  }
  pf.setPage(page);

  if ((!"".equals(pageType)||pageType==null)&&(!"".equals(barType)||barType==null)){
   nowCurrentBar = pf.getCurrentBar();
  }
  ArrayList listNews = (ArrayList) ndao.queryPage(page, pf.getPageSie());
  session.setAttribute("listNews", listNews);
  session.setAttribute("pageList", pf.paging());
  session.setAttribute("currentBar", nowCurrentBar);
  session.setAttribute("nowpage",page);
  //int currentBar = (pf.getPage()-1)/pf.getBarSize()+1;
  //session.setAttribute("currentBar",currentBar);
  return mapping.findForward("pageList");
  
 }

 
收藏助手
不良信息举报
您举报文章:JSP通用分页程序(测试版)
举报原因:
原因补充:

(最多只允许输入30个字)