1、定义属性保存相关的分页信息
private int currentPage; //当前页
private int totalPages; //总页数
private int pageRows; //每页记录数
private int totalRows; //总记录数
private int pageStartRows; //每页开始记录
private int pageEndRows; //每页结束记录
private boolean hasPreviousPage; //是否有上一页
private boolean hasNextPage; //是否有下一页
private List<Object[]> totalList; //要分页的数据
提供相应的getXXX()方法
2、属性初始化
public void initPageBean(List<Object[]> totalList, int pageRows){
this.totalList=totalList;
this.pageRows=pageRows;
this.totalRows=totalList.size();
this.currentPage=1;
//计算总页数
if((totalRows % pageRows)==0){
totalPages=totalRows / pageRows;
if(this.totalRows==0)
this.totalPages=1;
}else{
totalPages=totalRows / pageRows+1;
}
//默认无上一页
this.hasPreviousPage=false;
//判断是否有下一页
if(currentPage==totalPages){
hasNextPage=false;
}else{
hasNextPage=true;
}
//默认第一页开始的记录数为1
this.pageStartRow=1;
//确定第一页结束的记录数
if(totalRows<pageRows){
this.pageEndRow=totalRows;
}else{
this.pageEndRow=pageRows;
}
}
3、封装当前页的信息
public List<Object[]> getCurrentPageList(){
if(currentPage*pageRows<totalRows){
pageEndRow=currentPage*pageRows;
PageStartRow=pageEndRow-pageRows;
}else{
pageEndRow=totalRows;
pageStartRow=pageRows*(totalPages-1);
}
List<Object[]> pageList=new ArrayList<Object[]>(pageEndRow-pageStartRow+1);
if(totalRows!=0){
for(int i=pageStartRow; i<pageEndRow; i++){
pageList.add(totalList.get(i));
}
}
return pageList;
}
4、获取上一页的信息
public List<Object[]> getPreviousPageList(){
currentPage=currentPage-1;
if(currentPage<1)
currentPage=1;
if(currentPage>=totalPages){
hasNextPage=false;
}else{
hasNextPage=true;
}
if((currentPage-1)>0){
hasPreviousPage=true;
}else{
hasPreviousPage=false;
}
List<Object[]> pageList=this.getCurrentPageList();
return pageList;
5、获取下一页的信息
public List<Object[]> getNextPageList(){
currentPage=currentPage+1;
if(currentPage>totalPages){
currentPage=totalPages;
}
if((currentPage-1)>0){
hasPreviousPage=true;
}else{
hasPreviousPage=false;
}
if(currentPage>=totalPages){
hasNextPage=false;
}else{
hasNextPage=true;
}
List<Object[]> pageList=this.getCurrentPageList();
return pageList;
6、获取指定页信息
public List<Object[]> getAppointPageList(int currentPage){
this.currentPage=currentPage;
if(currentPage>this.totalPages)
this.currentPage=this.totalPages;
if(currentPage<1)
this.currentPage=1;
if(this.currentPage>1){
this.hasPreviousPage=true;
}else{
this.hasPreviousPage=false;
}
if(this.currentPage<this.totalPages){
this.hasNextPage=true;
}else{
this.hasNextPage=false;
}
List<Object[]> pageList=this.getCurrentPageList();
return pageList;
}
使用例:
<jsp:useBean id="pagination" class="com.mwq.tool.Pagination" scope="session" />
<form action="index.jsp" method="post" name="page" οnsubmit="return checkPage(page)">
<tr align="right">
<td>
共有<font color="red" size="1"><%=pagination.getTotalRows() %></font>条记录,
当前是第<font color="red" size="1">
<%=pagination.getCurrentPage()+"/"+pagination.getTotalPages() %)</font>页
<%if(pagination.isHasPreviousPage())
out.print("<a href='index.jsp?requestPage=previousPage'>上一页</a>");%>
<%if(pagination.isHasNextPage())
out.print("<a href="index.jsp?requestPage=nextPage'>下一页</a>");%>
第<input type="text" name="requestPage" size="3" maxlength="2">页
<input type="submit" value="转到>>">
</td>
</tr>
</form>
//对指定请求页时,需要对用户输入的页码进行校验
<script language="javascript" type="">
<!--
function checkPage(formName){
if(formName.requestPage.value==""){
alert("请填写欲跳转页码!");
formName.requestPage.focus();
return false;
}
if(isNaN(formName.requestPage.value)){
alert("欲跳转页码必须为数值!");
formName.requestPage.value="";
fromName.requestPage.focus();
return false;
}
}
//-->
</script>
//处理跳转页请求
List<Object[]> pageList=null;
if(request.getParameter("requestPage")==null{
List<Object[]> totalList=optb.selectBySQL("select * from tb_pagination");
pagination.initPageBean(totalList, 10);
pageList=pagination.getCurrentPageList();
}else{
String requestPage=request.getParameter("requestPage");
if(requestPage.equals("previousePage")){
pageList=pagination.getPreviousPageList();
}else if(requestPage.equals("nextPage")){
pageList=pagination.getNextPageList();
}else{
int appointPage=Integer.valueOf(requestPage);
pageList=pagination.getAppointPageList(appointPage);
}
}