下面第一個文件是pageSupport主要處理各種參數
package net.spring.service;
import java.util.List;
public class pageSupport {
int pagesize=2;//每頁條數
int totalCount;//總條數
int curPage;//當前頁數
int pageCount;//總頁數
List datas;//記錄集
public pageSupport(int startpage,int pagesizes,int count){
this.setPagesize(pagesizes);
this.setTotalCount(count);
this.pageCount=count/pagesizes+1;
this.setCurPage(startpage);
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getPagesize() {
return pagesize;
}
public void setPagesize(int pagesize) {
this.pagesize = pagesize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getCurPage() {
return curPage;
}
public void setCurPage(int curPage) {
if(curPage<=0)
curPage=0;
if(curPage>this.getPageCount())
curPage=this.getPageCount();
this.curPage = curPage;
}
public List getDatas() {
return datas;
}
public void setDatas(List datas) {
this.datas = datas;
}
}
這是調用方法
調用的時候傳入當前的頁面http://index.html?page=2 比如這個 傳入的是page這參數也就是 startpage這個變量對應的
[html]
int startpage=Integer.parseIn(response.Parameter(page)), pagesize=5
//this.geCount()是一個得到總記錄數的函數,自己寫。
pageSupport pSupport=new pageSupport(startpage,pagesize,this.getCount());
String sql="select * from table limit ?,?";
List list=//這裡需要自己把數據庫記錄讀出來並返回list形式 jdbcTemplate.queryForList(sql,
new Object[]{(pSupport.getCurPage()-1)*pagesize
,pagesize});
pSupport.setDatas(list);
[html] view plaincopy
<%--數字分頁形式開始 --%>
<%!
// @listNum=3;底下 1 2 3 等
// @thePageUrl:url拼湊字符串. 如:request.getRequestURI()+"?Page=";
// @pageClass="Page":當頁數字CSS類
// @pageCount:總頁數
// @showPage:當前要顯示的頁數
public String getPage(int listNum,String thePageUrl,String pageClass,int pageCount,int showPage)
{
String result="";
if(pageCount<=listNum)
{
for(int i=1;i<=pageCount;i++)
{
if(i==showPage)
{
result=result+"<span class="+pageClass+">"+i+"</span> ";
}
else
{
result=result+"<a href="+thePageUrl+i+">["+i+"]</a> ";
}
}
return result;
}
else if(pageCount>listNum)
{
//這裡根據showPage作出相應判斷
int FromListNum;
if(showPage%listNum!=0) //這裡根據求余判斷當前頁應該在那個分頁列表裡,如應該在1-5裡面還是6-10
{FromListNum=(showPage/listNum)*listNum+1;}
else
{FromListNum=(showPage/listNum)*listNum-listNum+1;}
int ToListNum=FromListNum+listNum-1;
if(pageCount>=ToListNum)
{
if(FromListNum!=1)
{
result=result+"<a href="+thePageUrl+"1 title=首頁><FONT face=webdings>9</FONT></a> ";
result=result+"<a href="+thePageUrl+(FromListNum-1)+" title=上"+listNum+"頁><FONT face=webdings>7</FONT></a> ";
}
else
{
result=result+"<FONT face=webdings>9</FONT> ";
result=result+"<FONT face=webdings>7</FONT> ";
}
for(int i=FromListNum;i<=ToListNum;i++)
{
if(i==showPage)
{
result=result+"<span class="+pageClass+">"+i+"</span> ";
}
else
{
result=result+"<a href="+thePageUrl+i+">["+i+"]</a> ";
}
}
result=result+"<a href="+thePageUrl+(ToListNum+1)+" title=下"+listNum+"頁><FONT face=webdings>8</FONT></a> ";
result=result+"<a href="+thePageUrl+pageCount+" title=末頁><FONT face=webdings>:</FONT></a> ";
return result;
}else
{
result=result+"<a href="+thePageUrl+"1 title=首頁><FONT face=webdings>9</FONT></a> ";
result=result+"<a href="+thePageUrl+(FromListNum-1)+" title=上"+listNum+"頁><FONT face=webdings>7</FONT></a> ";
for(int i=FromListNum;i<=pageCount;i++)
{
if(i==showPage)
{
result=result+"<span class="+pageClass+">"+i+"</span> ";
}
else
{
result=result+"<a href="+thePageUrl+i+">["+i+"]</a> ";
}
}
result=result+"<FONT face=webdings>8</FONT> ";
result=result+"<FONT face=webdings>:</FONT> ";
return result;
}
}
return "";
}
%>
<%--數字分頁形式結束 --%>