此代码为博主参考巴巴运动网源码所得,大部分一样,略有修改,在这里分享给大家,也方便自己以后写代码直接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. HDU 5012 Dice DFS

    简单DFS //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h ...

  2. 【集训笔记】贪心算法【HDOJ1052 【HDOJ2037

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  3. WPF中StringFormat 格式化 的用法

    原文 WPF中StringFormat 格式化 的用法 网格用法 <my:DataGridTextColumn x:Name="PerformedDate" Header=& ...

  4. Python 2.7 学习笔记 列表的使用

    同其它编程语言一样,python也提供了丰富的数据结构,以方便数据的处理.本文介绍两种最基本的数据集合,列表和元组的使用. 一.列表使用介绍 可以理解为一个有序的序列.其使用方式举例如下: list= ...

  5. grep egrep fgrep命令

    一.grep.egrep.fgrep命令 本文中主要介绍了linux系统下grep egrep fgrep命令和正则表达式的基本参数和使用格式.方法.(注释:文中fg代表例子,) 1.1.基本定义: ...

  6. 基于visual Studio2013解决C语言竞赛题之0522和为素

     题目

  7. 关于 Oracle外键列上是否需要索引问题?

    外键列上缺少索引会带来两个问题,限制并发性.影响性能.而这两个问题中的任意一个都可能会造成严重性能问题.   无论是Oracle的官方文档,还是在Tom的书中都说明了两种情况下可以忽略外键上的索引.其 ...

  8. HDU2191:悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(多重背包)

    Problem Description 急!灾区的食物依然短缺! 为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品 ...

  9. 打开sa属性报错

    --如果打开sa属性报错如下:无法显示请求的对话框.属性IsLocked不可用于“登录名sa".该对象可能没有此属性,也可能是访问权限不足而无法检索 --解决办法:首先用windows登录, ...

  10. ASP.NET页面之间传值

    介绍: 在网页应用程序的开发中,页面之间的传值应该是最常见的问题了. 在这篇文章里,azamsharp 将为我们介绍一些ASP.NET页面传值的方式.本文所举的例子非常简单,仅仅包含了一个文本框和几个 ...