分页工具类的定义与实现及测试。

发布时间:2023年12月18日

package d5.util;

public class PageUtil {
	private int pageSize;//一页有多少条
	private int currIndex;//当前是第几页
	private int totalCount;//共有多少条记录  谁给我? 逻辑层的 getTotalCount
	private int totalPage;//共有多少页	
	private int start;//显时时开始的位置
	
	
	//构造方法时,需要提供什么参数  一页多少个,当前第几页,一共多少条
	public PageUtil(int pageSize, int currIndex, int totalCount) {		
        currIndex=currIndex<1?1:currIndex;//如果当前页小于1,则为1
		this.pageSize = pageSize;
		this.currIndex = currIndex;
		this.totalCount = totalCount;
		
		
		totalPage=totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;
		currIndex=currIndex>totalPage?totalPage:currIndex;//如果当前页大于总页数	
		
		start=(currIndex-1)*pageSize;//开始的位置=(当前页-1)*一页的数
		
		
	}


	public int getPageSize() {
		return pageSize;
	}


	public int getCurrIndex() {
		return currIndex;
	}


	public int getTotalCount() {
		return totalCount;
	}


	public int getTotalPage() {
		return totalPage;
	}



	public int getStart() {
		return start;
	}


	@Override
	public String toString() {
		return "PageUtil [pageSize=" + pageSize + ", currIndex=" + currIndex + ", totalCount=" + totalCount
				+ ", totalPage=" + totalPage + ", start=" + start + ", toString()=" + super.toString() + "]";
	}

}

testZhiDingWeizhi4.jsp

<%@page import="d5.biz.IAdminuserBiz"%>
<%@page import="d5.biz.AdminuserBiz"%>
<%@page import="d5.entity.Adminuser"%>
<%@page import="java.util.*"%>
<%@page import="d5.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
    
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String page1=request.getParameter("page");
int num = 0;
if(page1==null)
	num =1;
else
	num = Integer.parseInt(page1);
if(num==0)
	num=1;

Adminuser u= new Adminuser(0,"","");
IAdminuserBiz ab=new AdminuserBiz();
List<Adminuser> list=new ArrayList<Adminuser>();
int count=ab.getTotalCount();
PageUtil pageUtil=new PageUtil(3,num,count);
list=ab.getUsersByPage(pageUtil.getStart(), pageUtil.getPageSize());
for(Adminuser user:list){
	out.println(user+"<br>");
}

%>
<a href="testZhiDingWeizhi4.jsp?page=1">第一页</a><br>

<a href="testZhiDingWeizhi4.jsp?page=<%=pageUtil.getCurrIndex()-1%>">上一页</a><br>
<a href="testZhiDingWeizhi4.jsp?page=<%=pageUtil.getCurrIndex()+1%>">下一页</a><br>

<a href="testZhiDingWeizhi4.jsp?page=<%=pageUtil.getTotalPage() %>">最后一页</a><br>
</body>
</html>

文章来源:https://blog.csdn.net/laocooon/article/details/134972625
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。