[置顶] 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 分页技术还区分两个:假分页和真分页 假分页:一次性从数据库读出表的所有数据一次性的返回给 ...
随机推荐
- 0x3f3f3f3f...编程中无穷大常量的设置技巧
转自 http://aikilis.tk/ 如果问题中各数据的范围明确,那么无穷大的设定不是问题,在不明确的情况下,很多程序员都取0x7fffffff作为无穷大,因为这是32-bit int的最大值. ...
- 关于ios下录音
http://blog.csdn.net/silencetq/article/details/8447400 我是采用的AVAudioRecorder这个框架来进行录音 这个录音跟官方网站上的spea ...
- 基于FPGA的DW8051移植(三)
总结一下问题: 1) http://www.cnblogs.com/sepeng/p/4137405.html 基于FPGA的DW8051移植(一)里面用modelsim观测波形发现程序进入了ida ...
- Socket网络编程--FTP客户端(60篇socket博客,而且都比较简单、深入浅出)
已经好久没有写过博客进行分享了.具体原因,在以后说. 这几天在了解FTP协议,准备任务是写一个FTP客户端程序.直接上干货了. 0.了解FTP作用 就是一个提供一个文件的共享协议. 1.了解FTP协议 ...
- JavaEE Tutorials (3) - 企业bean
3.1什么是企业bean383.1.1企业bean的好处393.1.2何时使用企业bean393.1.3企业bean类型393.2什么是会话bean393.2.1会话bean类型403.2.2何时使用 ...
- tweenanim动画
1.视图 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:too ...
- c# winform 弹出确认消息框判断是否删除?
if (MessageBox.Show("确认删除?", "此删除不可恢复", MessageBoxButtons.YesNo) == DialogResult ...
- BZOJ 1196: [HNOI2006]公路修建问题( MST )
水题... 容易发现花费最大最小即是求 MST 将每条边拆成一级 , 二级两条 , 然后跑 MST . 跑 MST 时 , 要先加 k 条一级road , 保证满足题意 , 然后再跑普通的 MST . ...
- 用eclipse编写Android程序时怎样生成apk文件
转载请注明出处:http://blog.csdn.net/ns_code/article/details/16828449 APK是Android Package的缩写,即Android安装包.通过将 ...
- 国产CPU走到十字路口:谁来取代英特尔芯片?(少写了一个OpenPower)
国内的几支CPU研发力量各自选择的指令体系都有自己的优点和问题,选择其中的哪一支都会有对应的成本和风险.最终谁能担大任,且拭目以待. 文 | 瞭望智库特约科技观察员 王强 用上内置国产CPU的个人电脑 ...