此代码为博主参考巴巴运动网源码所得,大部分一样,略有修改,在这里分享给大家,也方便自己以后写代码直接copy,看网上很多分页代码JSP里是用JAVA代码,博主原来也是这样,看到源码了解了JSTL,建议使用JSTL代码更清晰直观。

PageView.java
public class PageView<T> {
/** 分页数据 **/
private List<T> records;
/** 页码开始索引和结束索引 **/
private PageIndex pageindex;
/** 总页数 **/
private long totalpage = 1;
/** 每页显示记录数 **/
private int maxresult = 10;
/** 当前页 **/
private int currentpage = 1;
/** 总记录数 **/
private long totalrecord;
/** 页码数量 **/
private int pagecode = 7;
/** 要获取记录的开始索引 **/
public int getFirstResult() {
return (this.currentpage-1)*this.maxresult;
}
public int getPagecode() {
return pagecode;
} public void setPagecode(int pagecode) {
this.pagecode = pagecode;
} public void setQueryResult(List<T> qr){
setRecords(qr);
} public long getTotalrecord() {
return totalrecord;
}
public void setTotalrecord(long totalrecord) {
this.totalrecord = totalrecord;
setTotalpage(this.totalrecord%this.maxresult==0? this.totalrecord/this.maxresult : this.totalrecord/this.maxresult+1);
}
public List<T> getRecords() {
return records;
}
public void setRecords(List<T> records) {
this.records = records;
}
public PageIndex getPageindex() {
return pageindex;
}
public long getTotalpage() {
return totalpage;
}
public void setTotalpage(long totalpage) {
this.totalpage = totalpage;
this.pageindex = PageIndex.getPageIndex(pagecode, currentpage, totalpage);
}
public int getMaxresult() {
return maxresult;
}
public int getCurrentpage() {
return currentpage;
}
public void setMaxresult(int maxresult) {
this.maxresult = maxresult;
}
public void setCurrentpage(int currentpage) {
this.currentpage = currentpage;
}
}
PageIndex.java
public class PageIndex {
private long startindex;
private long endindex; public PageIndex(long startindex, long endindex) {
this.startindex = startindex;
this.endindex = endindex;
}
public long getStartindex() {
return startindex;
}
public void setStartindex(long startindex) {
this.startindex = startindex;
}
public long getEndindex() {
return endindex;
}
public void setEndindex(long endindex) {
this.endindex = endindex;
} public static PageIndex getPageIndex(long viewpagecount, int currentPage, long totalpage){
long startpage = currentPage-(viewpagecount%2==0? viewpagecount/2-1 : viewpagecount/2);
long endpage = currentPage+viewpagecount/2;
if(startpage<1){
startpage = 1;
if(totalpage>=viewpagecount) endpage = viewpagecount;
else endpage = totalpage;
}
if(endpage>totalpage){
endpage = totalpage;
if((endpage-viewpagecount)>0) startpage = endpage-viewpagecount+1;
else startpage = 1;
}
return new PageIndex(startpage, endpage);
}
}
PageDaoSupport.java //这个为参考李刚轻量级JAVA EE那本书
public class PageDaoSupport extends HibernateDaoSupport{
public List findByPage(final String hql, final int offset,
final int pageSize) {
// 通过一个HibernateCallback对象来执行查询
List list = getHibernateTemplate().executeFind(new HibernateCallback() {
// 实现HibernateCallback接口必须实现的方法
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
// 执行Hibernate分页查询
List result = session.createQuery(hql).setFirstResult(offset)
.setMaxResults(pageSize).list();
return result;
}
});
return list;
}
}
PagingServiceImpl.java
public class PagingServiceImpl implements PagingService {
private PageDaoSupport pageDaoSupport;
private PageView<ProductInfo> pageView;
private ProductInfoDao productInfoDao;//用来获得表中要显示的记录 public ProductInfoDao getProductInfoDao() {
return productInfoDao;
} public void setProductInfoDao(ProductInfoDao productInfoDao) {
this.productInfoDao = productInfoDao;
} public PageDaoSupport getPageDaoSupport() {
return pageDaoSupport;
} public void setPageDaoSupport(PageDaoSupport pageDaoSupport) {
this.pageDaoSupport = pageDaoSupport;
} public PageView<ProductInfo> getPageView() {
return pageView;
} public void setPageView(PageView<ProductInfo> pageView) {
this.pageView = pageView;
}
/**
* 首次请求时执行此方法
*/
public PageView<ProductInfo> paging() {
pageView.setCurrentpage(1);
pageView.setTotalrecord(productInfoDao.getTotalCount());
pageView.setQueryResult(pageDaoSupport.findByPage("from ProductInfo",
0, pageView.getMaxresult()));
return pageView;
}
/**
* 每次更换页码调用此方法
*/
public PageView<ProductInfo> requestPage(String requestPage) {
pageView.setCurrentpage(Integer.parseInt(requestPage));
pageView.setTotalrecord(productInfoDao.getTotalCount());
pageView.setQueryResult(pageDaoSupport.findByPage("from ProductInfo",
pageView.getFirstResult(), pageView.getMaxresult()));
return pageView;
}
}

代码差不多就这些了,解析下PageView.java

每页显示记录数maxresult,显示在页面的页码数pagecode,是写死的,我们设置下总记录数即可得到总页数,设置下当前页码,可得到pageindex页码开始索引和结束索引(根据总页数,当前页码,显示页码数),好了,只剩下最后一个变量了,即records我们要查找的记录集合,我在这里是每请求一页然后查询出当页的记录。至此,所有工作已完毕,显示到页面就可以了

显示页码的JSP

				<div class="pagination">

					<c:if test="${pageView.getCurrentpage()!=1}">
<a href="AdminShowProductAction!requestPage.action?pageNum=${pageView.getCurrentpage()-1}"
class="prev">«</a>
</c:if> <c:forEach begin="${pageView.getPageindex().getStartindex()}"
end="${pageView.getPageindex().getEndindex()}" var="x">
<a href="AdminShowProductAction!requestPage.action?pageNum=${x}"
<c:if test="${pageView.getCurrentpage()==x}">class="current"</c:if>>${x}
</a>
</c:forEach> <c:if test="${pageView.getCurrentpage()!= pageView.getTotalpage()}">
<a href="AdminShowProductAction!requestPage.action?pageNum=${pageView.getCurrentpage()+ 1}"
class="next">»</a>
</c:if>
</div>

[置顶] JSP分页,使用Hibernate+mysql的更多相关文章

  1. JSP+Spring+SpringMVC+Hibernate+Mysql实现的校园失物招领网站

    项目简介 项目来源于:https://github.com/wenlongup/LostAndFound 因原github仓库无数据库文件,经过本人修改,现将该仓库重新上传至个人gitee仓库. ht ...

  2. [置顶] JSP中使用taglib出错终极解决办法

    jsp中 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <c ...

  3. [置顶] jsp中c标签的使用

    jsp中c标签的使用 核心标签库 它是JSTL中的核心库,为日常任务提供通用支持,如显示和设置变量.重复使用一组项目.测试条件和其他操作(如导入和重定向Web内容).Core标签按功能可分为4种类型: ...

  4. [置顶] Jsp中的table多表头导出excel文件

    首先引入两份JS:copyhtmltoexcel.js以及 tableToExcel.js /* * 默认转换实现函数,如果需要其他功能,需自行扩展 * 参数: * tableID : HTML中Ta ...

  5. mysql分页查询按某类型置顶 按某类型置尾 再按优先级排序

    近段时间接到一个新需求: 第一优先级:未满的标的顺位高于已满标的顺位.第二优先级:新手标的顺位高于其他标的的顺位. 第三优先级:标的剩余可投金额少的顺位高于标的剩余可投金额多的. 我是直接通过sql语 ...

  6. mysql选择上一条、下一条数据记录,排序上移、下移、置顶

    1.功能须要 完毕列表排序上移,下移,置顶功能.效果例如以下图所看到的: 2设置思路 设置一个rank为之间戳,通过选择上移,就是将本记录与上一条记录rank值交换,下移就是将本条记录与下一条记录ra ...

  7. MySQL 上移/下移/置顶

    在编写网站系统时,难免会用到上移.下移.置顶的功能,今天小编就介绍一下我的思路. 首先,需要一张数据表: CREATE TABLE `a` ( `id` ) NOT NULL AUTO_INCREME ...

  8. JSP分页显示实例(基于Bootstrap)

    首先介绍一款简单利落的分页显示利器:bootstrap-paginator 效果截图: GitHub官方下载地址:https://github.com/lyonlai/bootstrap-pagina ...

  9. Jsp分页的简单制作

    Jsp分页的简单制作 运行环境:jsp+tomcat+eclipse 技术:servlet+jsp+mysql 分页技术还区分两个:假分页和真分页 假分页:一次性从数据库读出表的所有数据一次性的返回给 ...

随机推荐

  1. c#语法与c++ 及 java语法的对比分析

    早期开发的时候一直用c/c++,后来主要用的是java.最近需要用下c#. 熟悉了下c#,发现c#语言在对c/c++基础上做了很多简化,同时参考了很多java的语法习惯,本来在语法上c/c++就有很多 ...

  2. 转:MySql的commit和rollback

    从功能上划分,SQL 语言可以分为DDL,DML和DCL三大类.1. DDL(Data Definition Language)     数据定义语言,用于定义和管理 SQL 数据库中的所有对象的语言 ...

  3. 基于visual Studio2013解决C语言竞赛题之0514单词统计

     题目 解决代码及点评 /************************************************************************/ /* 14. 有一行字 ...

  4. 使用ffmpeg将BMP图片编码为x264视频文件,将H264视频保存为BMP图片,yuv视频文件保存为图片的代码

    ffmpeg开源库,实现将bmp格式的图片编码成x264文件,并将编码好的H264文件解码保存为BMP文件. 实现将视频文件yuv格式保存的图片格式的測试,图像格式png,jpg, gif等等測试均O ...

  5. QThread 与 QObject的关系(QObject可以用于多线程,可以发送信号调用存在于其他线程的slot函数,但GUI类不可重入)

    QThread 继承 QObject..它可以发送started和finished信号,也提供了一些slot函数. QObject.可以用于多线程,可以发送信号调用存在于其他线程的slot函数,也可以 ...

  6. Bootstrap技术: 模式对话框的使用

    一.概述 说到模式对话框,大家肯定都会想到windows下GUI程序,在gui程序中,有大量的对话框. 在web程序中,随着页面交互式功能的增多,有很多场景下也会用到对话框.在html原生的支持下,有 ...

  7. France '98

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=30506#problem/H #include<map> #include&l ...

  8. 【笔记】《通俗详细地讲解什么是P和NP问题》的概念记录

    1问题规模: 要计算或解决一个问题,该问题通常有一个大小规模,用n表示. 2算法的时间复杂度 计算次数与n的关系函数.(因为计算次数隐含时间). 3多项式时间复杂度 所有形如a*n^k+b*n^(k- ...

  9. CxSkinButton按钮皮肤类

    在codeproject 发现一个很强大的按钮皮肤类,之前的版本有内存泄露,但是作者已经修复了,原文网址是:http://www.codeproject.com/KB/buttons/cxskinbu ...

  10. Android Content Provider的启动过程源码分析

    本文參考Android应用程序组件Content Provider的启动过程源码分析http://blog.csdn.net/luoshengyang/article/details/6963418和 ...