大致流程:
Servlet从Jsp页面获取到当前页的页码,对当前页码进行判空处理,如果为空将其赋值为1
通过Dao层获取到请求数据的总行数
设置分页大小
将当前页,分页大小,总行数作为参数,创建一个PageUtil对象
Dao层获取PageUtil对象的当前页、分页大小,使用limit方法进行分页查找,返回数据
PageUtil对象封装了获取上一页、下一页、当前页、尾页的方法,将这些存入session中传入Jsp页面
封装分页:
public class PageUtil {
private String page; //当前页
private int pagesize=0; //分页数量
private int count=0; //总数据数
private int allpage=0; //总页数
private int pageindex=0;//当前页
private int nextpage=0; //下一页
private int prevpage=0; //上一页
private int lastpage=0; //尾页
public PageUtil(String page, int pagesize, int count) {
super();
this.page = page;
this.pagesize = pagesize;
this.count = count;
initAllpage(); //初始化总页数
initPageIndex();//初始化当前页
initPrevpage(); //初始化上一页
initNextpage(); //初始化下一页
initEndpage(); //初始化尾页
}
private void initPageIndex() {
if(page!=null&&page.equals("")){
pageindex = Integer.parseInt(page);
}else{
pageindex = 1;
pageindex = Integer.parseInt(page);
}
}
private void initEndpage() {
lastpage = allpage;
}
private void initNextpage() {
//如果当前页是尾页,则下一页也为尾页,其余都为当前页+1
if(pageindex>=allpage){
nextpage = allpage;
}else{
nextpage = pageindex+1;
}
}
private void initPrevpage() {
//如果当前页为1,则上一页也为1,其余都为当前页-1
if(pageindex>1){
prevpage = pageindex-1;
}else{
prevpage = 1;
}
}
private void initAllpage() {
if(count%pagesize==0){
allpage = count/pagesize;
}else{
allpage = count/pagesize+1;
}
}
public void init(){
}
public int getPageindex() {
return pageindex;
}
public void setPageindex(int pageindex) {
this.pageindex = pageindex;
}
public int getPagesize() {
return pagesize;
}
public void setPagesize(int pagesize) {
this.pagesize = pagesize;
}
public int getNextpage() {
return nextpage;
}
public void setNextpage(int nextpage) {
this.nextpage = nextpage;
}
public int getPrevpage() {
return prevpage;
}
public void setPrevpage(int prevpage) {
this.prevpage = prevpage;
}
public int getLastpage() {
return lastpage;
}
public void setLastpage(int lastpage) {
this.lastpage = lastpage;
}
}