struts+hibernate 分页

Pager类

import java.math.*;
public class Pager {
private int totalRows; //总行数
private int pageSize = 8; //每页显示的行数
private int currentPage; //当前页号
private int totalPages; //总页数
private int startRow; //当前页在数据库中的起始行
public Pager() {
}
public Pager(int _totalRows) {
totalRows = _totalRows;
totalPages=totalRows/pageSize;
int mod=totalRows%pageSize;
if(mod>0){
totalPages++;
}
currentPage = 1;
startRow = 0;

}


public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getStartRow() {
return startRow;
}
public void setStartRow(int startRow) {
this.startRow = startRow;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public int getTotalRows() {
return totalRows;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}

//转到任何一页
public void anyPage(int currentPage) {
this.currentPage=currentPage;
startRow=(currentPage-1)* pageSize;
}
//转到首页
public void first() {
currentPage = 1;
startRow = 0;
}
//转到上一页
public void previous() {
if (currentPage == 1) {
return;
}
currentPage--;
startRow = (currentPage - 1) * pageSize;
}
//转到下一页
public void next() {
if (currentPage < totalPages) {
currentPage++;
}
startRow = (currentPage - 1) * pageSize;
}
//转到尾页
public void last() {
currentPage = totalPages;
startRow = (currentPage - 1) * pageSize;
}
//设置当前页
public void refresh(int _currentPage) {
currentPage = _currentPage;
if (currentPage > totalPages) {
last();
}
}
}

PagerHelper类

public class PagerHelper {
public static Pager getPager(HttpServletRequest request,
int totalRows) {
//定义pager对象,用于传到页面
Pager pager = new Pager(totalRows);
//从Request对象中获取当前页号
String currentPage = request.getParameter("currentPage");
//如果当前页号为空,表示为首次查询该页
//如果不为空,则刷新pager对象,输入当前页号等信息
if (currentPage != null) {
pager.refresh(Integer.parseInt(currentPage));
}
//获取当前执行的方法,首页,前一页,后一页,尾页。
String pagerMethod = request.getParameter("pageMethod");
if (pagerMethod != null) {
if (pagerMethod.equals("first")) {
pager.first();
} else if (pagerMethod.equals("previous")) {
pager.previous();
} else if (pagerMethod.equals("next")) {
pager.next();
} else if (pagerMethod.equals("last")) {
pager.last();
}else{
pager.anyPage(Integer.parseInt(pagerMethod));
}
}
return pager;
}
}

Action

public class Download_findAllAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {

DownloadDao downloadDao=new DownloadDao();

int totalRows =downloadDao.getRows("from Download download");
Pager pager=PagerHelper.getPager(request,totalRows);

List list=downloadDao.findAll(pager.getStartRow(),pager.getPageSize());

request.setAttribute("result", list);
request.setAttribute("PAGER", pager);
return mapping.findForward("success");
}
}

Dao

public class DownloadDao{
//查找所有
public List findAll(int startRow,int pageSize) {
session = HibernateSessionFactory.getSession();
Query query=session.createQuery("from Download download order by download.id desc");
query.setFirstResult(startRow);
query.setMaxResults(pageSize);

List list=query.list();
HibernateSessionFactory.closeSession();
return list;
}

//返回查询总记录数
public int getRows(String hql){
session = HibernateSessionFactory.getSession();
Query query=session.createQuery(hql);
List list=query.list();
int totalRows=list.size();

//System.out.println(totalRows);
HibernateSessionFactory.closeSession();
return totalRows;
}
}

jsp页面

<tr>
<td width="40%" align="center">页次:<bean:write name="PAGER" property="currentPage" />/<bean:write name="PAGER" property="totalPages" /> 每页<bean:write name="PAGER" property="pageSize" /> 共计:<bean:write name="PAGER" property="totalRows" /> </td>
<td width="55%">

<a href="download_findAllAction.do?pageMethod=first&currentPage=<bean:write name='PAGER' property='currentPage'/>">首页</a>&nbsp;
<a href="download_findAllAction.do?pageMethod=previous&currentPage=<bean:write name='PAGER' property='currentPage'/>">上一页</a>&nbsp;
<a href="download_findAllAction.do?pageMethod=next&currentPage=<bean:write name='PAGER' property='currentPage'/>">下一页</a>&nbsp;
<a href="download_findAllAction.do?pageMethod=last&currentPage=<bean:write name='PAGER' property='currentPage'/>">尾页</a>&nbsp;

<select name="page" οnchange="location.href=this.options[this.selectedIndex].value;">

<%
Pager pager=(Pager)request.getAttribute("PAGER");
for (int i=1;i<=pager.getTotalPages();i++)
{
if(pager.getCurrentPage()==i)
{
%>
<option value="download_findAllAction.do?pageMethod=<%=i%>" selected="selected">
<%=i%>
</option>
<%
}else{
%>
<option value="download_findAllAction.do?pageMethod=<%=i%>">
<%=i%>
</option>
<%}%>
<%} %>
</select>


</td>



</tr>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值