欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

简单实现jsp分页

程序员文章站 2023-11-24 20:56:28
数据分页,在开发中必不可少,由于数据量太大,不得不进行处理。但是对于分页,又有很多种方式,比如使用sql语句的limit,或者使用jquery的插件。但是底层的逻辑,都是需...

数据分页,在开发中必不可少,由于数据量太大,不得不进行处理。但是对于分页,又有很多种方式,比如使用sql语句的limit,或者使用jquery的插件。但是底层的逻辑,都是需要的。
在这里贴出bean对象的代码,以及一个辅助类。我做的这个项目是一个个人博客,算是个人网站吧。

(只贴部分底层逻辑代码)

page.java

package cn.edu.ldu.util;

public class page {

  private int everypage;     //每页显示的数量
  private int totalpage;     //总页数
  private int totalcount;     //总数量
  private int currentpage;    //当前页
  private int beginindex;     //起始页
  private boolean hasprepage;   //是否有上一页
  private boolean hasnextpage;  //是否有下一页

  public page() {
  }

  public page(int everypage, int totalpage, int totalcount, int currentpage,int beginindex, boolean hasprepage, boolean hasnextpage)
  {
    this.everypage = everypage;
    this.totalpage = totalpage;
    this.totalcount = totalcount;
    this.currentpage = currentpage;
    this.beginindex = beginindex;
    this.hasprepage = hasprepage;
    this.hasnextpage = hasnextpage;
  }

  public int geteverypage() {
    return everypage;
  }

  public void seteverypage(int everypage) {
    this.everypage = everypage;
  }

  public int gettotalpage() {
    return totalpage;
  }

  public void settotalpage(int totalpage) {
    this.totalpage = totalpage;
  }

  public int gettotalcount() {
    return totalcount;
  }

  public void settotalcount(int totalcount) {
    this.totalcount = totalcount;
  }

  public int getcurrentpage() {
    return currentpage;
  }

  public void setcurrentpage(int currentpage) {
    this.currentpage = currentpage;
  }

  public int getbeginindex() {
    return beginindex;
  }

  public void setbeginindex(int beginindex) {
    this.beginindex = beginindex;
  }

  public boolean ishasprepage() {
    return hasprepage;
  }

  public void sethasprepage(boolean hasprepage) {
    this.hasprepage = hasprepage;
  }

  public boolean ishasnextpage() {
    return hasnextpage;
  }

  public void sethasnextpage(boolean hasnextpage) {
    this.hasnextpage = hasnextpage;
  }

}

pageutil.java

package cn.edu.ldu.util;

public class pageutil {
  //创建分页信息对象
  public static page createpage(int everypage,int totalcount,int currentpage) {
    everypage = geteverypage(everypage);
    currentpage = getcurrentpage(currentpage);
    int totalpage = gettotalpage(everypage, totalcount);
    int beginindex = getbeginindex(everypage, currentpage);
    boolean hasprepage = gethasprepage(currentpage);
    boolean hasnextpage = gethasnextpage(totalpage, currentpage);
    return new page(everypage, totalpage,totalcount, currentpage,
        beginindex, hasprepage, hasnextpage);
  }

  //获得每页显示记录数
  public static int geteverypage(int everypage) {
    return everypage == 0 ? 10 : everypage;
  }

  //获得当前页
  public static int getcurrentpage(int currentpage) {
    return currentpage == 0 ? 10 : currentpage;
  }

  //获得总页数
  public static int gettotalpage(int everypage,int totalcount) {
    int totalpage=0;
    //要判断每页是否满页,否则页数+1
    if(totalcount!=0 && totalcount%everypage==0) {
      totalpage=totalcount/everypage;
    }
    else {
      totalpage=totalcount/everypage+1;
    }
    return totalpage;
  }

  //获得起始位置
  public static int getbeginindex(int everypage,int currentpage) {
    return everypage*(currentpage-1);
  }

  //判断是否有上一页
  public static boolean gethasprepage(int currentpage) {
    return currentpage== 1 ? false : true;

  }

  //判断是否有下一页
  public static boolean gethasnextpage(int totalpage, int currentpage) {
    return (currentpage == totalpage) || (totalpage == 0) ? false : true;
  }

}


每次我只需要去调用即可。

//传入每页显示的页数,总记录数,当前页
page page=pageutil.createpage(5, diarydao.findallcount(), currentpage);

简单实现jsp分页

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。