JAVA分页代码实例_第1页
JAVA分页代码实例_第2页
JAVA分页代码实例_第3页
JAVA分页代码实例_第4页
JAVA分页代码实例_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 JAVA分页代码实例package com.hjf.web.view; public class PageUtil . private int pageSize;/每页显示的条数 private int recordCount;/总共的条数 private int currentPage;/当前页面 public PageUtil(int pageSize, int recordCount, int currentPage) . this.pageSize = pageSize; this.recordCount = recordCount; setCurrentPage(currentPa

2、ge); /构造方法 public PageUtil(int pageSize, int recordCount) . this(pageSize, recordCount, 1); /总页数 public int getPageCount() . int size = recordCount/pageSize;/总条数/每页显示的条数=总页数 int mod = recordCount % pageSize;/最后一页的条数 if(mod != 0) size+; return recordCount = 0 ? 1 : size; /包含,起始索引为0 public int getFrom

3、Index() . /System.out.println("from index:"+(currentPage-1) * pageSize); return (currentPage-1) * pageSize; /不包含 public int getToIndex() . /System.out.println("to index:"+Math.min(recordCount, currentPage * pageSize); return Math.min(recordCount, currentPage * pageSize); /得到当前页 p

4、ublic int getCurrentPage() . return currentPage; /设置当前页 public void setCurrentPage(int currentPage) . int validPage = currentPage <= 0 ? 1 : currentPage; validPage = validPage > getPageCount() ? getPageCount() : validPage; this.currentPage = validPage; /得到每页显示的条数 public int getPageSize() . ret

5、urn pageSize; /设置每页显示的条数 public void setPageSize(int pageSize) . this.pageSize = pageSize; /得到总共的条数 public int getRecordCount() . return recordCount; /设置总共的条数 public void setRecordCount(int recordCount) . this.recordCount = recordCount; - 下面的代码是放在jsp里面的 - <% PublishersDAO dao = PublishersDAO.getI

6、nstance(); List records = dao.getModels(); String pageStr = request.getParameter("page"); int currentPage = 1; if (pageStr != null) currentPage = Integer.parseInt(pageStr); PageUtil pUtil = new PageUtil(10, records.size(), currentPage); currentPage = pUtil.getCurrentPage(); %> -下面这个是放在有

7、变量的上面- <% for (int i = pUtil.getFromIndex(); i < pUtil.getToIndex(); i+) PublisherModel model = (PublisherModel) records.get(i); %> 中间是删除修改之类的代码 <%> -这个是结尾的- <tr><td width=100% bgcolor="#eeeeee" colspan=4 align="center"> 记录总数<%=pUtil.getRecordCount()

8、%>条 当前页/总页数<%=currentPage%> /<%=pUtil.getPageCount()%>每页显示<%=pUtil.getPageSize()%>条 <a href="publishers.jsp?page=1">首页</a> <a href="publishers.jsp?page=<%=(currentPage - 1)%>">上页</a> <a href="publishers.jsp?page=<%=(cu

9、rrentPage + 1)%>">下页</a> <a href="publishers.jsp?page=<%=pUtil.getPageCount()%>">末页</a> </td></tr> = java分页代码 package mon; import java.util.Vector; /* * 公共的列表 */ public class CommonList extends Vector /* * 当前页数 */ public int pageNo; /* * 每页显示记

10、录数 */ public int pageSize; /* * 总页数 */ public int pageNum; /* * 总纪录数 */ public int recNum; /* * 开始记录数 */ public int startPos; /* * 结束记录数 */ public int endPos; /* * 默认页大小 */ public final static int defaultPageSize = 20; public static final String COMMONLIST_TAG_KEY = "commonlist_tag_key" pu

11、blic CommonList() public CommonList(int pageNo, int pageSize) if (pageNo < 0) pageNo = 0; this.pageNo = pageNo; this.pageSize = pageSize; /* * 计算其它属性 * param recNum 总纪录数 * param pageNo 当前页 * param pageSize 页大小 */ public CommonList(int recNum, int pageNo, int pageSize) calculate(recNum, pageNo, pa

12、geSize); /* * 计算其它属性 * param recNum 总纪录数 */ public void calculate(int recNum) calculate(recNum, pageNo, pageSize); /* * 计算其它属性 * param recNum 总纪录数 * param pageNo 当前页 * param pageSize 页大小 */ public void calculate(int recNum, int pageNo, int pageSize) if (recNum < 1) return; if (pageSize = 0) retur

13、n; if (pageNo < 1) pageNo = 1; this.pageNo = pageNo; this.pageSize = pageSize; this.recNum = recNum; if (pageSize > 0) if (recNum % pageSize > 0) pageNum = recNum / pageSize + 1; else pageNum = recNum / pageSize; if (pageNo > pageNum) this.pageNo = pageNum; if (this.pageNo < 1) this.p

14、ageNo = 1; startPos = (this.pageNo - 1) * pageSize + 1; endPos = this.pageNo * pageSize; else startPos = 0; endPos = recNum; /* * 显示分页标志 * param cl 列表 * return 结果 */ public static String getPage(CommonList cl) return getPage(cl, 10, null); /* * 显示分页标志 * param cl 列表 * param pageNum 显示的数目 * return 结果

15、*/ public static String getPage(CommonList cl, int pageNum) return getPage(cl, pageNum, null); /* * 显示分页标志 * param cl 列表 * param pageNum 显示的数目 * param strPage 分页参数 * return 结果 */ public static String getPage(CommonList cl, int pageNum, String strPage) String rValue = null; StringBuffer sb = new Stri

16、ngBuffer(); try if (cl != null) if (strPage != null && !strPage.equals("") strPage = ","" + strPage + """ else strPage = "" if (pageNum < 1) pageNum = 10; sb.append("共有<font color='#FF0000'>" + cl.recNum + "<

17、;/font>条记录," + cl.pageNo + "/" + cl.pageNum + "页。"); if (cl.pageNo > 1) sb.append(" <a href='" + strPage + ")'>|&lt;</a> <a href='" + (cl.pageNo - 1) + strPage + ")'>&lt;</a>"); int currentNu

18、m = (cl.pageNo % pageNum = 0 ? (cl.pageNo / pageNum) - 1 : (int) (cl.pageNo / pageNum) * pageNum; if (currentNum < 0) currentNum = 0; if (cl.pageNo > pageNum) sb.append(" <a href='" + (currentNum - pageNum + 1) + strPage + ")'>.</a>"); for (int i = 0; i

19、< pageNum; i+) if (currentNum + i + 1) > cl.pageNum | cl.pageNum < 2) break; sb.append(" <a href='" + (currentNum + i + 1) + strPage + ")'>" + (currentNum + i + 1 = cl.pageNo ? "<font color='#FF0000'><b>" + (currentNum + i + 1)

20、 + "</b></font>" : (currentNum + i + 1) + "") + "</a>"); if (cl.pageNum > (currentNum + pageNum) sb.append(" <a href='" + (currentNum + 1 + pageNum) + strPage + ")'>.</a>"); if (cl.pageNo < cl.pageNum) sb.a

21、ppend(" <a href='" + (cl.pageNo + 1) + strPage + ")'>&gt;</a> <a href='" + cl.pageNum + strPage + ")'>&gt;|</a>"); rValue = sb.toString(); else rValue = "" catch (Exception e) rValue = "" finally sb = n

22、ull; / cl = null; return rValue; public String toString() return "mon.CommonList" + "pageNo=" + pageNo + ", pageSize=" + pageSize + ", pageNum=" + pageNum + ", recNum=" + recNum + ", startPos=" + startPos + ", endPos=" + endPos +

23、"" /*请保留下面的注释*/ /* js 翻页代码 function tunePage(toPageNo,pageNo) try var topage = 1; if(typeof(toPageNo) != "number" | toPageNo < 1) topage = 1; else topage = toPageNo; var olds = window.location.searchSubject; if(typeof(pageNo) = "undefined" | pageNo = "") pa

24、geNo = "pageNo" var news = "" if(olds.length > 1) olds = olds.substring(1,olds.length); var arrays = olds.split("&"); for (var i = 0; i < arrays.length ; i+) if(arraysi.indexOf(pageNo + "=") < 0 && arraysi.length > 1) news += "&

25、;" + arraysi; if(news.length > 1) news = "?" + news.substring(1,news.length) + "&" + pageNo + "=" + topage; else news = "?" + pageNo + "=" + topage; else news = "?" + pageNo + "=" + topage; window.location = window.loc

26、ation.pathname + news; catch(e) window.location = window.location.pathname + window.location.searchSubject; function sAll(thisObj,dObj) try var l = eval(dObj + ".length"); if(typeof(l) = "undefined") eval(dObj).checked = thisObj.checked; else for(var i = 0; i < l; i+) if(!eval

27、(dObj + "" + i + "").disabled) eval(dObj + "" + i + "").checked = thisObj.checked; catch(e) */ JSP代码: CommonList zxcl=db.search(1,"","",phototypename,"" , "" , orderby , ascdesc , pageNo, pageSize); <table width="

28、;100%" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="D6E2FF"> <tr> <form method="get"> <td align="right"><%=CommonList.getPage(cl)%></td> </form> </tr> </ta

29、ble> DB.JAVA代码: public CommonList search(int status, String playername, String pictitle, String phototypename, String beginDate, String endDate, String orderby, String ascdesc, int pageNo, int pageSize) throws Exception CommonList lst; Connection conn; PreparedStatement st; ResultSet rst; Excepti

30、on exception; lst = null; StringBuffer buffer = new StringBuffer(1024); if(status > -1) buffer.append(" and (status=" + status + ")"); if(playername != null && playername.trim().length() > 0) buffer.append(" and (playername like '%" + playername + &quo

31、t;%')"); if(pictitle != null && pictitle.trim().length() > 0) buffer.append(" and (pictitle like '%" + pictitle + "%')"); if(phototypename != null && phototypename.trim().length() > 0) buffer.append(" and (phototypename = '" +

32、phototypename + "')"); if(beginDate != null && beginDate.length() > 0) buffer.append(" and (inputdate>=to_date('" + beginDate + "','yyyy-mm-dd')"); if(endDate != null && endDate.length() > 0) endDate = endDate + " 23:59:

33、59" buffer.append(" and (inputdate<=to_date('" + endDate + "','yyyy-mm-dd HH24:MI:SS')"); String str_orderby = "" if(orderby != null && orderby.trim().length() > 0) str_orderby = " order by " + orderby; else str_orderby = &qu

34、ot; order by inputdate " if(ascdesc != null && ascdesc.trim().length() > 0) str_orderby = str_orderby + " " + ascdesc; else str_orderby = str_orderby + " desc " String wherestr = buffer.length() > 0 ? buffer.replace(1, 5, " where ").toString() : "

35、;" String countSQL = "select count(*) from furniturepicture " + wherestr; String querySQL = "select * from (select a.*,rownum rowno from (select * from furniturepicture " + wherestr + str_orderby + " ) a) where rowno between ? and ?" int recordCount = 0; int pageCo

36、unt = 0; conn = null; st = null; rst = null; try conn = Conn.getConnection("search"); conn.setAutoCommit(false); st = conn.prepareStatement(countSQL); rst = st.executeQuery(); if(rst.next() recordCount = rst.getInt(1); st.close(); st = null; rst.close(); rst = null; pageCount = recordCount

37、 / pageSize; if(recordCount % pageSize != 0) pageCount+; if(pageNo < 1) pageNo = 1; else if(pageNo > pageCount) pageNo = pageCount; if(pageNo < 1) pageNo = 1; lst = new CommonList(recordCount, pageNo, pageSize); st = conn.prepareStatement(querySQL); st.setInt(1, pageSize * (pageNo - 1) + 1)

38、; st.setInt(2, pageSize * pageNo); FurniturePicture obj; for(rst = st.executeQuery(); rst.next(); lst.add(obj) obj = new FurniturePicture(); obj.setFurniturepictureid(rst.getLong("furniturepictureid"); obj.setPlayerid(rst.getLong("playerid"); obj.setPlayername(rst.getString("playername"); obj.setIdcard(rst.getString("idcard"); obj.setTel(rst.getString("tel"); obj.setEmail(rst.getString("email&qu

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论