您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 其它行业文档 > JAVA分页-使用多种方法实现分页技术
////在struts中分页的一种实现//////////我的项目中的分页功能1,思路使用一个页面控制类,它记录页面信息,如上页,下页,当前页等。在查询的Action中,将这个控制类和查询条件一起传递给数据库访问bean,然后将这两个参数保存在用户session中。在分页控制Action中,利用接收到的分页参数调用数据库访问的bean.2,实现(1)分页控制类/*@authornick*Createdon2004-3-18*filename:PageController.java***/packagecom.tower.util;/***@authornick*2004-3-18*用来进行翻页控制**/publicclassPageController{inttotalRowsAmount;//总行数booleanrowsAmountSet;//是否设置过totalRowsAmountintpageSize=2;//每页行数intcurrentPage=1;//当前页码intnextPage;intpreviousPage;inttotalPages;//总页数booleanhasNext;//是否有下一页booleanhasPrevious;//是否有前一页Stringdescription;intpageStartRow;intpageEndRow;publicPageController(inttotalRows){setTotalRowsAmount(totalRows);}publicPageController(){}/***@parami*设定总行数*/publicvoidsetTotalRowsAmount(inti){if(!this.rowsAmountSet){totalRowsAmount=i;totalPages=totalRowsAmount/pageSize+1;setCurrentPage(1);this.rowsAmountSet=true;}}/***@parami**当前页**/publicvoidsetCurrentPage(inti){currentPage=i;nextPage=currentPage+1;previousPage=currentPage-1;//计算当前页开始行和结束行if(currentPage*pageSizetotalRowsAmount){pageEndRow=currentPage*pageSize;pageStartRow=pageEndRow-pageSize+1;}else{pageEndRow=totalRowsAmount;pageStartRow=pageSize*(totalPages-1)+1;}//是否存在前页和后页if(nextPagetotalPages){hasNext=false;}else{hasNext=true;}if(previousPage==0){hasPrevious=false;}else{hasPrevious=true;};System.out.println(this.description());}/***@return*/publicintgetCurrentPage(){returncurrentPage;}/***@return*/publicbooleanisHasNext(){returnhasNext;}/***@return*/publicbooleanisHasPrevious(){returnhasPrevious;}/***@return*/publicintgetNextPage(){returnnextPage;}/***@return*/publicintgetPageSize(){returnpageSize;}/***@return*/publicintgetPreviousPage(){returnpreviousPage;}/***@return*/publicintgetTotalPages(){returntotalPages;}/***@return*/publicintgetTotalRowsAmount(){returntotalRowsAmount;}/***@paramb*/publicvoidsetHasNext(booleanb){hasNext=b;}/***@paramb*/publicvoidsetHasPrevious(booleanb){hasPrevious=b;}/***@parami*/publicvoidsetNextPage(inti){nextPage=i;}/***@parami*/publicvoidsetPageSize(inti){pageSize=i;}/***@parami*/publicvoidsetPreviousPage(inti){previousPage=i;}/***@parami*/publicvoidsetTotalPages(inti){totalPages=i;}/***@return*/publicintgetPageEndRow(){returnpageEndRow;}/***@return*/publicintgetPageStartRow(){returnpageStartRow;}publicStringgetDescription(){Stringdescription=Total:+this.getTotalRowsAmount()+items+this.getTotalPages()+pages;//this.currentPage+Previous+this.hasPrevious+//Next:+this.hasNext+//startrow:+this.pageStartRow+//endrow:+this.pageEndRow;returndescription;}publicStringdescription(){Stringdescription=Total:+this.getTotalRowsAmount()+items+this.getTotalPages()+pages,Currentpage:+this.currentPage+Previous+this.hasPrevious+Next:+this.hasNext+startrow:+this.pageStartRow+endrow:+this.pageEndRow;returndescription;}publicstaticvoidmain(Stringargs[]){PageControllerpc=newPageController(3);System.out.println(pc.getDescription());//pc.setCurrentPage(2);//System.out.println(pc.description());//pc.setCurrentPage(3);//System.out.println(pc.description());}}(2)查询Action的代码片断publicActionForwardexecute(ActionMappingmapping,ActionFormform,HttpServletRequestrequest,HttpServletResponseresponse)throwsException{BasequeryForm=(Base)form;if(!queryForm.getName().equals()){PageControllerpc=newPageController();EmployeeBaseservice=newEmployeeBase();ArrayListresult=(ArrayList)service.search(queryForm,pc);HttpSessionsession=request.getSession();session.setAttribute(queryForm,queryForm);session.setAttribute(pageController,service.getPageController());request.setAttribute(queryResult,result);request.setAttribute(pageController,service.getPageController());returnmapping.findForward(haveResult);}else{returnmapping.findForward(noResult);}}(3),翻页Action的代码片断publicActionForwardexecute(ActionMappingmapping,ActionFormform,HttpServletRequestrequest,HttpServletResponseresponse)throwsException{//读取翻页参数TurnPageFormturnPageForm=(TurnPageForm)form;//从PageController中取出查询信息,并使用bean提供的调用接口处理结果HttpSessionsession=request.getSession();PageControllerpc=(PageController)session.getAttribute(pageController);BasequeryForm=(Base)session.getAttribute(queryForm);pc.setCurrentPage(turnPageForm.getViewPage());EmployeeBaseservice=newEmployeeBase();ArrayListresult=(ArrayList)service.search(queryForm,pc);//根据参数将数据写入requestrequest.removeAttribute(queryResult);request.removeAttribute(pageController);request.setAttribute(queryResult,result);request.setAttribute(pageController,pc);//forward到显示页面returnmapping.findForward(haveResult);}(4)数据库访问bean中的片断publicCollectionsearch(Basebase,PageControllerpc)throwsSQLException{ArrayListemps=newArrayList();ResultSetrs=getSearchResult(base);rs.absolute(-1);pc.setTotalRowsAmount(rs.getRow());setPageController(pc);if(rs.getRow()0){rs.absolute(pc.getPageStartRow());do{System.out.println(inloop+rs.getRow());Baseb=newBase();b.setName(rs.getString(Name));b.setIdCard(rs.getString(IDCard));System.out.println(Fromdb:+rs.getString(IDCard));emps.add(b);if(!rs.next()){break;}}while(rs.getRow()(pc.getPageEndRow()+1));}returnemps;}(5)在jsp中,翻页部分的代码片
本文标题:JAVA分页-使用多种方法实现分页技术
链接地址:https://www.777doc.com/doc-4304242 .html