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¤tPage=<bean:write name='PAGER' property='currentPage'/>">首頁</a>?
????????<a href="download_findAllAction.do?pageMethod=previous¤tPage=<bean:write name='PAGER' property='currentPage'/>">上一頁</a>?
????????<a href="download_findAllAction.do?pageMethod=next¤tPage=<bean:write name='PAGER' property='currentPage'/>">下一頁</a>?
????????<a href="download_findAllAction.do?pageMethod=last¤tPage=<bean:write name='PAGER' property='currentPage'/>">尾頁</a>?
????????<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>
總結
以上是生活随笔為你收集整理的struts+hibernate 分页的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: javascript 二级动态下拉菜单选
- 下一篇: 控制文字长度,多出的文字用省略号代替