首页 技术 正文
技术 2022年11月18日
0 收藏 608 点赞 2,126 浏览 4814 个字

此代码为博主参考巴巴运动网源码所得,大部分一样,略有修改,在这里分享给大家,也方便自己以后写代码直接copy[置顶] JSP分页,使用Hibernate+mysql,看网上很多分页代码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}">
<ahref="AdminShowProductAction!requestPage.action?pageNum=${pageView.getCurrentpage()-1}" rel="external nofollow"
class="prev">«</a>
</c:if><c:forEach begin="${pageView.getPageindex().getStartindex()}"
end="${pageView.getPageindex().getEndindex()}" var="x">
<a href="AdminShowProductAction!requestPage.action?pageNum=${x}" rel="external nofollow"
<c:if test="${pageView.getCurrentpage()==x}">class="current"</c:if>>${x}
</a>
</c:forEach><c:if test="${pageView.getCurrentpage()!= pageView.getTotalpage()}">
<ahref="AdminShowProductAction!requestPage.action?pageNum=${pageView.getCurrentpage()+ 1}" rel="external nofollow"
class="next">»</a>
</c:if>
</div>
下一篇: Python-线程(1)
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,020
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,359
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,142
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,772
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,850