import java.util.List; /** * @author Administrator * * */ public class PageControl { private int prevPage = 1; private int nextPage = 1; private int pageCount = 1; private int pageNum = 1; private int recordCount = 0; private int pageSize = 20; private List list; private int start; public PageControl() { } /** * @param li:列表 * @param count:记录集总数 * @param page:当前页 * @param s * @param ps */ public PageControl(List li, int count, int page, int s, int ps) { this.recordCount = count; this.pageNum = page; if (page < 1) { this.pageNum = 1; } this.pageSize = ps; adjust(); this.list = li; this.start = s; } private void adjust() { pageCount = (int) ((float) recordCount / pageSize + 0.99); pageNum = (pageNum > 0 ? pageNum : 1); pageNum = (pageNum > pageCount ? pageCount : pageNum); nextPage = (pageNum < pageCount ? (pageNum + 1) : pageCount); prevPage = (pageNum > 1 ? (pageNum - 1) : 1); } /** * 返回分页控制器 */ public String getShowPage() { StringBuffer sb = new StringBuffer("\n"); if (pageCount <= 1) { sb.append("<input type=hidden name=ipage>"); return sb.toString(); } if (pageNum > 1) { sb.append("<a href=\"javascript:GoPage(1)\">"); sb .append("<img src=\"/images/firs_button.gif\" border=\"0\" valign=\"absbottom\"></a> "); sb.append("<a href=\"javascript:GoPage(" + (pageNum - 1) + ")\">"); sb .append("<img src=\"/images/prev_button.gif\" border=\"0\" valign=\"absbottom\"></a>\n"); } if (pageNum < pageCount) { sb.append(" <a href=\"javascript:GoPage(" + (pageNum + 1) + ")\">"); sb .append("<img src=\"/images/next_button.gif\" border=\"0\" valign=\"absbottom\"></a> "); sb.append("<a href=\"javascript:GoPage(" + pageCount + ")\">"); sb .append("<img src=\"/images/last_button.gif\" border=\"0\" valign=\"absbottom\"></a>\n"); } if (pageCount > 1) { sb.append(" 共" + recordCount + "条记录 分" + pageCount + "页 每页" + pageSize + "条\n"); sb .append(" 当前第<select name=\"ipage\" onchange=\"GoPage(this.value)\">\n"); for (int i = 1; i <= pageCount; i++) { if (i == pageNum) { sb.append("<option selected value=\"" + i + "\">" + i + "</option>\n"); } else { sb.append("<option value=\"" + i + "\">" + i + "</option>\n"); } } sb.append("</select>页 \n"); } return sb.toString(); } public int getNextPage() { return nextPage; } public int getFirstNo() { return (pageNum - 1) * pageSize + 1; } /** * 每一页大小 * * @return */ public int getPageSize() { return pageSize; } public int getPageCount() { return pageCount; } /** * 当前第几页页面 * * @return */ public int getPageNum() { return pageNum; } public int getPrevPage() { return prevPage; } /** * 总记录数 * * @return */ public int getRecordCount() { return recordCount; } public void setPageSize(int i) { pageSize = i; } public List getList() { return list; } /** * 从第几条开始 * * @return */ public int getStart() { return start; } /** * 输出分页显示的结果. * * @param pageNum * 当前页面 * @param recordCount * 所有结果 * @param pageSize * 一页显示的多少 * @param pageCountSize * 前后跳页的多少 * @param linkpageurl * 连接页面的 URL 字符串 * @return 分页结果的字符串. */ public String paging(int pageCountSize, String linkpageurl) { int PageCount = -1; // 页面总数 String LinkPageName = linkpageurl; String LinkText = ""; int StartPage; int TempPage; int TempPageCount; TempPage = (pageNum - 1) % pageCountSize; // StartPage = pageNum - TempPage; // TempPageCount = recordCount % pageSize; if (TempPageCount == 0) { PageCount = recordCount / pageSize; } else { PageCount = (recordCount / pageSize) + 1; // } String txtPrev = "前" + pageCountSize + "页"; String txtNext = "后" + pageCountSize + "页"; String txtStart = "首页"; String txtEnd = "末页"; // 首页 // if (StartPage - 1 > 0) { if (pageNum - 1 > 0) { LinkText += "<a href='" + LinkPageName + "&page=1' title='到首页'>" + txtStart + "</a>"; } else { // LinkText += txtStart; LinkText += ""; } // 到前10页 if (StartPage - 1 > 0) { LinkText += "<a href='" + LinkPageName + "&page=" + (StartPage - 1) + "' title='到第" + pageCountSize + "页'>" + txtPrev + "</a>"; } else { // LinkText += txtPrev; LinkText += ""; } for (int i = StartPage; i < StartPage + pageCountSize; i++) { if (i < PageCount + 1) { if (i == pageNum) { LinkText += "<a class='currentlyPage' href='" + LinkPageName + "&page="; LinkText += i + "' title='" + i + "页'>"; LinkText += i; } else { LinkText += "<a href='" + LinkPageName + "&page="; LinkText += i + "' title='" + i + "页'>"; LinkText += i; } LinkText += "</a>"; } } // 中间页面 if (StartPage + pageCountSize - PageCount - 1 < 0) { LinkText += "<a href='" + LinkPageName + "&page=" + (StartPage + pageCountSize) + "' title='到第" + pageCountSize + "页'>" + txtNext + "</a>"; } else { // LinkText += txtNext; LinkText += ""; } // 最后一页 // if (StartPage + pageCountSize <= PageCount) { if (pageNum < PageCount) { LinkText += "<a href='" + LinkPageName + "&page=" + PageCount + "' title='最后一页'>" + txtEnd + "</a>"; } else { // LinkText += txtEnd; LinkText += ""; } if(recordCount < pageSize) LinkText = ""; return LinkText; } }
调用例子
public PageControl getPageControl(String sql, int page, int pageSize) throws SQLException { List list = new ArrayList(); int start = 0; int pageCount = 0; int recordCount = 0; Connection conn = null; PreparedStatement prepare = null; ResultSet rs = null; ConnectionPool pool = ConnectionPool.getInstance(); try { conn = pool.getConnection(); prepare = conn.prepareStatement(sql); rs = prepare.executeQuery(); rs.last(); if (rs.isLast()) { recordCount = rs.getRow(); } rs.beforeFirst(); pageCount = (recordCount + pageSize - 1) / pageSize; if (page > pageCount) page = pageCount; start = (page - 1) * pageSize + 1; boolean bOk = true; if (start == 1) bOk = true; else bOk = rs.absolute(start - 1); int i = 1, j = 0; while (bOk && rs.next() && j < pageSize) { ContentInfoForm form = new ContentInfoForm(); form.setId(rs.getLong(i)); i++; form.setContentId(rs.getLong(i)); i++; form.setContentTypeId(rs.getInt(i)); i++; form.setBlogId(rs.getLong(i)); i++; form.setBlogName(rs.getString(i)); i++; form.setTitle(rs.getString(i)); i++; form.setPublic(rs.getBoolean(i)); i++; form.setReply(rs.getBoolean(i)); i++; form.setBrowseNum(rs.getInt(i)); i++; form.setReplyNum(rs.getInt(i)); i++; form.setMainIndex(rs.getBoolean(i)); i++; form.setSubIndex(rs.getBoolean(i)); i++; form.setChoiceIndex(rs.getBoolean(i)); i++; form.setRegDate(rs.getTimestamp(i)); list.add(form); i = 1; j++; } } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); } } finally { try { if (prepare != null) { prepare.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { pool.free(conn); } } } } PageControl pcontrol = new PageControl(list, recordCount, page, start, pageSize); return pcontrol; }
分页例子
PageControl pControl = dao.getPageControl(sql.toString(), ipage, 20); List list = pControl.getList(); String showPage = pControl.paging(10, "/admin/contentTypeList.do?method=untype“; request.setAttribute("list", list); request.setAttribute("showPage", showPage); request.setAttribute("search", search); request.setAttribute("type", type);
<style> /*---分页S---*/ #pagination_box { clear: both; overflow: hidden; height: 35px; text-align: center; margin-top: 10px; zoom:1; line-height: 35px; } #pagination_box a { border: 1px solid #BACBDD; margin-right: 2px; margin-left: 2px; padding-top: 2px; padding-right: 5px; padding-bottom: 2px; padding-left: 5px; overflow: hidden; font-family: "宋体"; } #pagination_box a:link { color: #497098; } #pagination_box a:visited { color: #497098; } #pagination_box a:hover { color: #FFFFFF; background-color: #658CB4; text-decoration: none; } #pagination_box a:active { color: #497098; } #pagination_box .currentlyPage:link { color: #ffffff; background-color: #658CB4; } #pagination_box .currentlyPage:visited { color: #ffffff; background-color: #658CB4;; } #pagination_box .currentlyPage:hover { color: #ffffff; background-color: #658CB4; } #pagination_box .currentlyPage:active { color: #ffffff; background-color: #658CB4; } #pagination_box .PB_input { width: 20px; overflow: hidden; border: 1px solid #CCCCCC; vertical-align: middle; } #pagination_box .PB_but { cursor:hand; padding-left: 5px; vertical-align: middle; padding-bottom: 5px; margin-top:4px; } /*---分页E---*/ </style> <div id="pagination_box"><%=showPage %></div> <!--分页E-->