简单实现jsp分页
#代码知识 发布时间: 2026-01-11
数据分页,在开发中必不可少,由于数据量太大,不得不进行处理。但是对于分页,又有很多种方式,比如使用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);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
代码知识SEO上一篇 : jQuery中的100个技巧汇总
下一篇 : php 三元运算符实例详细介绍
-
SEO外包最佳选择国内专业的白帽SEO机构,熟知搜索算法,各行业企业站优化策略!
SEO公司
-
可定制SEO优化套餐基于整站优化与品牌搜索展现,定制个性化营销推广方案!
SEO套餐
-
SEO入门教程多年积累SEO实战案例,从新手到专家,从入门到精通,海量的SEO学习资料!
SEO教程
-
SEO项目资源高质量SEO项目资源,稀缺性外链,优质文案代写,老域名提权,云主机相关配置折扣!
SEO资源
-
SEO快速建站快速搭建符合搜索引擎友好的企业网站,协助备案,域名选择,服务器配置等相关服务!
SEO建站
-
快速搜索引擎优化建议没有任何SEO机构,可以承诺搜索引擎排名的具体位置,如果有,那么请您多注意!专业的SEO机构,一般情况下只能确保目标关键词进入到首页或者前几页,如果您有相关问题,欢迎咨询!