您的当前位置:首页正文

java分页

2022-12-31 来源:客趣旅游网
javaÀàµÄ´úÂ룺

**************************************************************************

package web.tool;

import java.util.ArrayList;

public class PageBean {

private ArrayList list = new ArrayList(); private int prePage; private int nextPage; private int totalPage; private int currPage;

public ArrayList getList() { return list; }

public void setList(ArrayList list) { this.list = list; }

public int getPrePage() { return prePage; }

public void setPrePage(int prePage) { this.prePage = prePage; }

public int getNextPage() { return nextPage; }

public void setNextPage(int nextPage) { this.nextPage = nextPage;

}

public int getTotalPage() { return totalPage; }

public void setTotalPage(int totalPage) { this.totalPage = totalPage; }

public PageBean(ArrayList list,String pageNOStr, int countPerPage) {

int pageNO; try {

pageNO = Integer.parseInt(pageNOStr); }catch(Exception ex) { pageNO = 1; }

pageNO = pageNO < 1 ? 1 : pageNO;

int totalCount = list.size(); int totalPage;

if(totalCount == 0) { totalPage = 1; }else {

totalPage =totalCount % countPerPage == 0 ? totalCount/countPerPage : totalCount/countPerPage + 1; }

pageNO = pageNO > totalPage ? 1 : pageNO;

this.currPage = pageNO;

this.prePage = pageNO == 1 ? 1 : pageNO - 1;

this.nextPage = pageNO == totalPage ? totalPage : pageNO + 1; this.totalPage = totalPage; int count = 0;

for(int i = (pageNO - 1)*countPerPage; i < list.size(); i++) {

if(count==countPerPage){ break; }

this.list.add(list.get(i));

count++; } }

public int getCurrPage() { return currPage; }

public void setCurrPage(int currPage) { this.currPage = currPage; } }

**************************************************************************

jspÒ³ÃæµÄ´úÂ룺

**************************************************************************

<%@ page language=\"java\" import=\"java.util.*,entity.*,biz.*,web.tool.*\" pageEncoding=\"GBK\"%> <%

MessageBiz msgBiz = new MessageBiz();

ArrayList msgs = msgBiz.getAllMessages();

PageBean pageBean = new PageBean(msgs,request.getParameter(\"pageNO\"),3); // 3´ú±íÿҳÏÔʾ¼¸ÌõÊý¾Ý£¬Ö»¸ÄÕâ¸ö msgs = pageBean.getList(); %>

jsp¶¯Ì¬·ÖÒ³¼¼Êõ

<%for(Message m : msgs) { %>

<%}%>

href=\"/msg/index.jsp?pageNO=<%=pageBean.getPrePage() %>\" >ÉÏÒ»Ò³

href=\"/msg/index.jsp?pageNO=<%=pageBean.getNextPage() %>\" >ÏÂÒ»Ò³

onClick=\"location.href='/msg/index.jsp?pageNO=<%=pageBean.getPrePage() %>';\">ÉÏÒ»Ò³ onClick=\"location.href='/msg/index.jsp?pageNO=<%=pageBean.getNextPage() %>';\">ÏÂÒ»Ò³

<%=pageBean.getCurrPage() %> / <%=pageBean.getTotalPage() %>

因篇幅问题不能全部显示,请点此查看更多更全内容