作者:未知 文章來(lái)源:http://www./ 訪問(wèn)次數(shù): 次 加入時(shí)間:2005-01-19 在struts中分頁(yè)的一種實(shí)現(xiàn)
我的項(xiàng)目中的分頁(yè)功能 1,, 思路
使用一個(gè)頁(yè)面控制類,,它記錄頁(yè)面信息,如上頁(yè),,下頁(yè),,當(dāng)前頁(yè)等。在查詢的Action中,,將這個(gè)控制類和查詢條件一起傳遞給數(shù)據(jù)庫(kù)訪問(wèn)bean,,然后將這兩個(gè)參數(shù)保存在用戶session中。在分頁(yè)控制Action中,,利用接收到的分頁(yè)參數(shù)調(diào)用數(shù)據(jù)庫(kù)訪問(wèn)的bean.
2,實(shí)現(xiàn)
(1)分頁(yè)控制類 /* @author nick * Created on 2004-3-18 * file name:PageController.java * * */ package com.tower.util;
/** * @author nick * 2004-3-18 * 用來(lái)進(jìn)行翻頁(yè)控制 * */ public class PageController { int totalRowsAmount; //總行數(shù) boolean rowsAmountSet; //是否設(shè)置過(guò)totalRowsAmount int pageSize=2; //每頁(yè)行數(shù) int currentPage=1; //當(dāng)前頁(yè)碼 int nextPage; int previousPage; int totalPages; //總頁(yè)數(shù) boolean hasNext; //是否有下一頁(yè) boolean hasPrevious; //是否有前一頁(yè) String description; int pageStartRow; int pageEndRow;
public PageController(int totalRows){ setTotalRowsAmount(totalRows); } public PageController(){}
/** * @param i * 設(shè)定總行數(shù) */ public void setTotalRowsAmount(int i) { if(!this.rowsAmountSet){ totalRowsAmount = i; totalPages=totalRowsAmount/pageSize+1; setCurrentPage(1); this.rowsAmountSet=true; }
}
/** * @param i * * 當(dāng)前頁(yè) * */ public void setCurrentPage(int i) { currentPage = i; nextPage=currentPage+1; previousPage=currentPage-1; //計(jì)算當(dāng)前頁(yè)開(kāi)始行和結(jié)束行 if(currentPage*pageSize<totalRowsAmount){ pageEndRow=currentPage*pageSize; pageStartRow=pageEndRow-pageSize+1;
}else{ pageEndRow=totalRowsAmount; pageStartRow=pageSize*(totalPages-1)+1; }
//是否存在前頁(yè)和后頁(yè)
if (nextPage>totalPages){ hasNext=false; }else{ hasNext=true; } if(previousPage==0){ hasPrevious=false; }else{ hasPrevious=true; }; System.out.println(this.description()); }
/** * @return */ public int getCurrentPage() { return currentPage; }
/** * @return */ public boolean isHasNext() { return hasNext; }
/** * @return */ public boolean isHasPrevious() { return hasPrevious; }
/** * @return */ public int getNextPage() { return nextPage; }
/** * @return */ public int getPageSize() { return pageSize; }
/** * @return */ public int getPreviousPage() { return previousPage; }
/** * @return */ public int getTotalPages() { return totalPages; }
/** * @return */ public int getTotalRowsAmount() { return totalRowsAmount; }
/** * @param b */ public void setHasNext(boolean b) { hasNext = b; }
/** * @param b */ public void setHasPrevious(boolean b) { hasPrevious = b; }
/** * @param i */ public void setNextPage(int i) { nextPage = i; }
/** * @param i */ public void setPageSize(int i) { pageSize = i; }
/** * @param i */ public void setPreviousPage(int i) { previousPage = i; }
/** * @param i */ public void setTotalPages(int i) { totalPages = i; } /** * @return */ public int getPageEndRow() { return pageEndRow; }
/** * @return */ public int getPageStartRow() { return pageStartRow; }
public String getDescription(){ String description="Total:"+this.getTotalRowsAmount()+ " items "+this.getTotalPages() +" pages"; // this.currentPage+" Previous "+this.hasPrevious + // " Next:"+this.hasNext+ // " start row:"+this.pageStartRow+ // " end row:"+this.pageEndRow; return description; }
public String description(){ String description="Total:"+this.getTotalRowsAmount()+ " items "+this.getTotalPages() +" pages,Current page:"+ this.currentPage+" Previous "+this.hasPrevious + " Next:"+this.hasNext+ " start row:"+this.pageStartRow+ " end row:"+this.pageEndRow; return description; }
public static void main(String args[]){ PageController pc=new PageController(3); System.out.println(pc.getDescription()); // pc.setCurrentPage(2); // System.out.println(pc.description()); // pc.setCurrentPage(3); // System.out.println(pc.description()); }
}
(2)查詢Action的代碼片斷
public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { Base queryForm= (Base) form;
if(!queryForm.getName().equals("")){ PageController pc=new PageController(); EmployeeBase service=new EmployeeBase(); ArrayList result=(ArrayList)service.search(queryForm,pc);
HttpSession session=request.getSession();
session.setAttribute("queryForm",queryForm); session.setAttribute("pageController",service.getPageController());
request.setAttribute("queryResult",result); request.setAttribute("pageController",service.getPageController()); return mapping.findForward("haveResult"); }else{ return mapping.findForward("noResult"); }
}
(3),,翻頁(yè)Action的代碼片斷
public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
//讀取翻頁(yè)參數(shù)
TurnPageForm turnPageForm=(TurnPageForm)form;
//從PageController中取出查詢信息,并使用bean提供的調(diào)用接口處理結(jié)果
HttpSession session=request.getSession(); PageController pc=(PageController)session.getAttribute("pageController"); Base queryForm=(Base)session.getAttribute("queryForm");
pc.setCurrentPage(turnPageForm.getViewPage());
EmployeeBase service=new EmployeeBase();
ArrayList result=(ArrayList)service.search(queryForm,pc);
//根據(jù)參數(shù)將數(shù)據(jù)寫(xiě)入 request
request.removeAttribute("queryResult"); request.removeAttribute("pageController"); request.setAttribute("queryResult",result); request.setAttribute("pageController",pc);
//forward 到顯示頁(yè)面
return mapping.findForward("haveResult");
}
(4)數(shù)據(jù)庫(kù)訪問(wèn)bean中的片斷
public Collection search(Base base, PageController pc) throws SQLException { ArrayList emps = new ArrayList(); ResultSet rs = getSearchResult(base);
rs.absolute(-1); pc.setTotalRowsAmount(rs.getRow()); setPageController(pc); if (rs.getRow() > 0) {
rs.absolute(pc.getPageStartRow());
do { System.out.println("in loop" + rs.getRow());
Base b = new Base(); b.setName(rs.getString("Name")); b.setIdCard(rs.getString("IDCard")); System.out.println("From db:" + rs.getString("IDCard")); emps.add(b); if (!rs.next()) { break; } } while (rs.getRow() < (pc.getPageEndRow() + 1)); } return emps; }
(5)在jsp中,,翻頁(yè)部分的代碼片斷
<bean:write name="pageController" property="description"/>
<logic:equal name="pageController" property="hasPrevious" value="true"> <a href="turnPage.do?viewPage=<bean:write name="pageController" property="previousPage"/>" class="a02"> Previous </a> </logic:equal>
<logic:equal name="pageController" property="hasNext" value="true"> <a href="turnPage.do?viewPage=<bean:write name="pageController" property="nextPage"/>" class="a02"> Next </a> </logic:equal>
這樣一來(lái),翻頁(yè)的功能可以以你喜歡的方式表現(xiàn)給client
|
|