簡單實用,拿到這裡和大家一起分享. package page; import java.util.List; import java.util.Vector; public class Pagination { private int sumPageNumber; private int sumItemNumber; private int pageNumber; private int showItemNumber; private String visitPageURL; private String pageHTML; public List interceptListByStarItemNumber(List itemList) { int startItem = (pageNumber-1)*showItemNumber; List tpItemList = new Vector(); if (itemList == null) { return null; } sumItemNumber=itemList.size(); if(sumItemNumber%showItemNumber==0){ sumPageNumber =sumItemNumber/showItemNumber; }else{ sumPageNumber =sumItemNumber/showItemNumber+1; } for (int i = 0; i < itemList.size(); i++) { // 不要超過截取的最大條數, if ((i) == (startItem + showItemNumber)) { break; } // 當迴圈到開始條數的時候,將對象添加到新的列表裡面。 if ((i) >= startItem) { tpItemList.add(itemList.get(i)); } } return tpItemList; } |
public String buildHTML(String tWidth) {
String tableWidth=tWidth;
String visitFirstPageURL="";
String visitNextPageURL="";
String visitPreviousURL="";
String visitEndURL="";
visitPageURL=visitPageURL+"?showItemNumber="+showItemNumber+"&pageNumber=";
if(pageNumber==1){
visitFirstPageURL="[首頁] ";
visitPreviousURL="[上一頁] ";
}else{
visitFirstPageURL="<a href='"+visitPageURL+"1'>[首頁]</a> ";
visitPreviousURL="<A href='"+visitPageURL+(pageNumber-1)+"'>[上一頁]</A> ";
}
if(pageNumber==sumPageNumber){
visitNextPageURL="[下一頁] ";
visitEndURL="[尾頁] ";
}else{
visitNextPageURL="<a href='"+visitPageURL+(pageNumber+1)+"'>[下一頁]</a> ";
visitEndURL="<a href='"+visitPageURL+sumPageNumber+"'>[尾頁]</a>";
}
StringBuffer sb = new StringBuffer();
sb.append("<table width="+tableWidth);
sb.append("><tr><td align=right>");
sb.append("當前是第["+pageNumber+"]頁 ");
sb.append("共["+sumPageNumber+"]頁 "+"共["+sumItemNumber+"]條 ");
sb.append(visitFirstPageURL);
sb.append(visitPreviousURL);
sb.append(visitNextPageURL);
sb.append(visitEndURL);
sb.append("</td> </tr></table>");
return sb.toString();
}
public int getPageNumber() {
return pageNumber;
}
public void setPageNumber(int pageNumber) {
this.pageNumber = pageNumber;
}
public int getShowItemNumber() {
return showItemNumber;
}
public void setShowItemNumber(int showItemNumber) {
this.showItemNumber = showItemNumber;
}
public int getSumItemNumber() {
return sumItemNumber;
}
public int getSumPageNumber() {
return sumPageNumber;
}
public String getVisitPageURL() {
return visitPageURL;
}
public void setVisitPageURL(String visitPageURL) {
this.visitPageURL = visitPageURL;
}
public String getPageHTML() {
return pageHTML;
}
}
JSP頁面調用:
<%@ page import="page.*"%>
<%@ page import="java.util.*"%>
<%@ page contentType="text/html; charset=GBK" %>
jsp分頁組件:
<%
Vector list=new Vector();
for(int i=0;i<100;i++)
list.add(i+" ");
Pagination pagination = null;
String pageNumber = request.getParameter("pageNumber");
int showItemNumber = 5;
if (pageNumber == null) {
pageNumber = "1";
}
String HTML = "";
if (list != null && list.size() > 0) {
pagination = new Pagination();
pagination.setPageNumber(Integer.parseInt(pageNumber));
pagination.setShowItemNumber(showItemNumber);
pagination.setVisitPageURL("Allshetuan.jsp");
list =(Vector) pagination.interceptListByStarItemNumber(list);
for(int i=0;i<list.size();i++)
{
out.print("<br>"+list.get(i));
}
HTML = pagination.buildHTML("600");
out.print(HTML);
}
%>