[置顶] JSP分页,使用Hibernate+mysql
此代码为博主参考巴巴运动网源码所得,大部分一样,略有修改,在这里分享给大家,也方便自己以后写代码直接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的更多相关文章
- JSP+Spring+SpringMVC+Hibernate+Mysql实现的校园失物招领网站
项目简介 项目来源于:https://github.com/wenlongup/LostAndFound 因原github仓库无数据库文件,经过本人修改,现将该仓库重新上传至个人gitee仓库. ht ...
- [置顶] JSP中使用taglib出错终极解决办法
jsp中 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <c ...
- [置顶] jsp中c标签的使用
jsp中c标签的使用 核心标签库 它是JSTL中的核心库,为日常任务提供通用支持,如显示和设置变量.重复使用一组项目.测试条件和其他操作(如导入和重定向Web内容).Core标签按功能可分为4种类型: ...
- [置顶] Jsp中的table多表头导出excel文件
首先引入两份JS:copyhtmltoexcel.js以及 tableToExcel.js /* * 默认转换实现函数,如果需要其他功能,需自行扩展 * 参数: * tableID : HTML中Ta ...
- mysql分页查询按某类型置顶 按某类型置尾 再按优先级排序
近段时间接到一个新需求: 第一优先级:未满的标的顺位高于已满标的顺位.第二优先级:新手标的顺位高于其他标的的顺位. 第三优先级:标的剩余可投金额少的顺位高于标的剩余可投金额多的. 我是直接通过sql语 ...
- mysql选择上一条、下一条数据记录,排序上移、下移、置顶
1.功能须要 完毕列表排序上移,下移,置顶功能.效果例如以下图所看到的: 2设置思路 设置一个rank为之间戳,通过选择上移,就是将本记录与上一条记录rank值交换,下移就是将本条记录与下一条记录ra ...
- MySQL 上移/下移/置顶
在编写网站系统时,难免会用到上移.下移.置顶的功能,今天小编就介绍一下我的思路. 首先,需要一张数据表: CREATE TABLE `a` ( `id` ) NOT NULL AUTO_INCREME ...
- JSP分页显示实例(基于Bootstrap)
首先介绍一款简单利落的分页显示利器:bootstrap-paginator 效果截图: GitHub官方下载地址:https://github.com/lyonlai/bootstrap-pagina ...
- Jsp分页的简单制作
Jsp分页的简单制作 运行环境:jsp+tomcat+eclipse 技术:servlet+jsp+mysql 分页技术还区分两个:假分页和真分页 假分页:一次性从数据库读出表的所有数据一次性的返回给 ...
随机推荐
- BFS 、DFS 解决迷宫入门问题
问题 B: 逃离迷宫二 时间限制: 1 Sec 内存限制: 128 MB提交: 12 解决: 5[提交][状态][讨论版] 题目描述 王子深爱着公主.但是一天,公主被妖怪抓走了,并且被关到了迷宫. ...
- 【深圳,武汉】一加科技(One Plus)招聘,寻找不...
[深圳,武汉]一加科技(One Plus)招聘,寻找不... [深圳,武汉]一加科技(One Plus)招聘,寻找不... 来自: 一加 2013-12-30 15:28:04 标题: ...
- eclipse处理长字符串拼接快捷方法类
情景: 你在后台写sql文访问数据库时是不是要这样写 String sql="select a," +"b," +"c " +"f ...
- W5100使用中的常见问题
来自:成都浩然 越来越多的嵌入式网络系统project师喜欢上了W5100,它集TCP/IP协议栈.以太网的MAC和PHY一体,不仅使系统性能得到非常大的提升,也给产品开发工作带来极大的方便.随着W5 ...
- Menu的自己定义实现-------保卫萝卜造塔升级塔菜单实现
cocos2dx原生的menu排版函数实现的非常无完整,像最主要的Item的排序要想做得略微美丽一些就须要我们自己实现. 对于Menu我们能够用两种方法来实现: 1.大神级别. 继承自Control, ...
- C++算术运算符与算术表达式
基本的算术运算符 在本章中主要介绍算术运算符与算术表达式,赋值运算符与赋值表达式,逗号运算符与逗号表达式,其他运算符将在以后各章中陆续介绍. 常见算数运算符 运算符 说明 举例 + 加法运算符,或正值 ...
- C语言顺序栈实现
/*数序栈*/ #include<stdio.h> #include<stdlib.h> #include<math.h> #define SElemType ch ...
- JVM调优总结(二)-一些概念
Java对象的大小 基本数据的类型的大小是固定的,这里就不多说了.对于非基本类型的Java对象,其大小就值得商榷. 在Java中,一个空Object对象的大小是8byte,这个大小只是保存堆中一个没有 ...
- CentOS 6.2 二进制安装apache2.4.3出现configure: error: APR-util not found. Please read the documentation的解决方
CentOS 6.2 二进制安装apache2.4.3出现configure: error: APR-util not found. Please read the documentation的解决方 ...
- Swift - 网络请求报App Transport Security has blocked a cleartext错
使用Xcode7编写iOS9应用时,如果获取http://数据时会报如下错误: App Transport Security has blocked a cleartext HTTP (http:// ...