一种分页方法:
package com.huawei.api.site.util;
//接口类
public interface PageBase {
public void setPageSize(int pageSize); //设置分页大小
public int getPageSize(); //返回分页大小
public int getPageCount(); //返回总页数
public int getPageRowsCount(); //返回当前页的记录条数
public void setPageRowsCount(); //获得每页的条数
public void setRowsCount(); //设置总记录行数
public int getRowsCount(); //返回总记录行数
public int getCurPage(); //返回当前页号
public void setCurPage(int curPage_);//设置当前页号
public void setCurPageListData();//设置当前页面显示的记录集合对象
// public void gotoPage(int page); //转到指定页
// public void pageFirst() throws SQLException; //转到当前页的第一条记条并说明异常
// public void pageLast() throws SQLException; //转到当前页的最后一条记条并说明异常
}
package com.huawei.api.site.util;
import java.util.ArrayList;
import java.util.List;
//实现接口的类
public class Pagetion implements PageBase {
private int rowsCount; // 总记录条数
private int pageSize; // 分页的大小,即每页的最大条数
private int curPage; // 当前页
private int pageRowsCount;
private List curPageList;
private List totalList;
public void setRowsCount() {
if(this.totalList!=null) {
int itempRowCount = totalList.size();
this.rowsCount = itempRowCount;
} else {
this.rowsCount=0;
}
}
public int getCurPage() { // 获得当前页
return curPage;
}
public void setCurPage(int curPage_) {
this.curPage = curPage_;
}
public int getPageCount() { // 获得总页数
if (rowsCount == 0) {
return 0;
}
if (pageSize == 0) {
return 1;
}
if (rowsCount % pageSize == 0) {
return rowsCount / pageSize;
} else {
return rowsCount / pageSize + 1;
}
}
public int getRowsCount() { // 获得总记录条数
return rowsCount;
}
public void setPageRowsCount() { // 获得每页的条数
if (pageSize == 0) {
this.pageRowsCount = rowsCount;
} else if (getRowsCount() == 0) {
this.pageRowsCount = 0;
} else if (curPage != getPageCount()) {
this.pageRowsCount = pageSize;
} else {
this.pageRowsCount = rowsCount - (getPageCount() - 1) * pageSize;
}
}
public int getPageRowsCount() { // 获得每页的条数
return this.pageRowsCount;
}
public int getPageSize() { // 获得分页的大小
return pageSize;
}
public void setTotalList(List list) {
this.totalList=list;
}
public List getTotalList(){
return this.totalList;
}
public void gotoPage(int page) { //到达第X页
if(totalList==null){
return;
}
if(curPage<1) {
curPage=1;
}
if(curPage>getPageCount()) {
curPage=getPageCount();
} else {
curPage=page;
}
//int row=(page-1)*pageSize+1;
}
/*
* public void gotoPage(int page) { //到达第X页 if(rs==null) return; if(page<1) {
* page=1; } if(page>getPageCount()) { page=getPageCount(); } int
* row=(page-1)*pageSize+1; try{ rs.absolute(row); curPage=page;
* }catch(SQLException e) { } }
*
* public void pageFirst() throws SQLException { int
* row=(curPage-1)*pageSize+1; rs.absolute(row); }
*
* public void pageLast() throws SQLException { int
* row=(curPage-1)*pageSize+getPageRowsCount(); rs.absolute(row); }
*/
public void setPageSize(int pageSize) { // 设置分页大小
if (pageSize >= 0) {
this.pageSize = pageSize;
//curPage = 1;
}
}
public List getCurPageList() {
return curPageList;
}
public void setCurPageListData() {
if(this.rowsCount>0) {
List curPageList = new ArrayList();
if ((this.rowsCount > (this.curPage-1) * this.pageSize) && (this.curPage>=1)) {
for (int i=(this.curPage-1)* this.pageSize; i < this.curPage * this.pageSize; i++) {
if (i < this.rowsCount) {
curPageList.add(this.totalList.get(i));
}
}
}
this.curPageList=curPageList;
}else if(this.totalList==null) {
this.curPageList=null;
} else if(this.totalList!=null) {
if(this.totalList.size()==0){
this.curPageList=totalList;
}
}
}
}
//使用方法
//一、在action中
//创建分页对象
Pagetion Pagetion=new Pagetion();
//设置当前页
Pagetion.setCurPage(ipage);
//set所有记录
Pagetion.setTotalList(vc);
//set每个页面大小
Pagetion.setPageSize(3);
Pagetion.setRowsCount();
Pagetion.setPageRowsCount();
Pagetion.setCurPageListData();
request.setAttribute("Pagetion", Pagetion);
//二、在jsp中
Pagetion page=(Pagetion)request.getAttribute("Pagetion");
//通过Pagetion的getCurPageList()返回一个List,得到结果当前页的List
<div align="center">
<span class="STYLE5"><a>第<s:property value="curPage" />页
共<s:property value="pageCount" />页
</a></span>
<span class="STYLE5"><a href='<s:url action="ShowApiByProtocol.action"><s:param
name="protocol" value="protocol" ></s:param>
<s:param name="curPage">1</s:param></s:url>'> 首页</a></span>
<s:if test="curPage>1">
<span class="STYLE5"><a href='<s:url action="ShowApiByProtocol.action"><s:param
name="protocol" value="protocol" ></s:param>
<s:param name="curPage" value="%{curPage-1}"></s:param></s:url>'> 上一页</a></span>
</s:if>
<s:if test="pageCount>curPage">
<span class="STYLE5"><a href='<s:url action="ShowApiByProtocol.action"><s:param
name="protocol" value="protocol" ></s:param>
<s:param name="curPage" value="%{curPage+1}"></s:param></s:url>'> 下一页</a></span>
</s:if>
<span class="STYLE5"><a href='<s:url action="ShowApiByProtocol.action"><s:param
name="protocol" value="protocol" ></s:param>
<s:param name="curPage" value="pageCount"></s:param></s:url>'> 尾页</a></span>
</div>