public final class Pager { private int intCurrentPage = 1; // 当前页 private int intTotal = -1; // 记录总数 private int intPosition = -1; // 记录游标 private int intPageSize = Constants.CON_PAGESIZE; // 分页记录值 private int intPageTotal = -1; // 页总数 private String searchString = ""; // 查询条件串 private String method = null; public void setPageSize(int num) { intPageSize = num; } /** * 每页记录数 * * @return */ public int getPageSize() { return intPageSize > 0 ? intPageSize : Constants.CON_PAGESIZE; } /** * 页面开始记录的游标 * * @return */ public int getPosition() { intPosition = (getCurrentPage() - 1) * getPageSize(); if (intPosition < 0) { intPosition = 0; } return intPosition; } /** * 设置记录总数 * * @param total */ public void setTotal(int total) { intTotal = total; } public long getTotal() { return intTotal > 0 ? intTotal : 0; } /** * 总页数. * * @return */ public int getPageTotal() { intPageTotal = (int) Math.ceil((double) getTotal() / (double) getPageSize()); return intPageTotal > 0 ? intPageTotal : 0; } /** * 设置页号 * * @param pageno */ public void setCurrentPage(String pageno) { if (pageno != null && !pageno.equals("")) { this.intCurrentPage = Integer.valueOf(pageno).intValue(); } } /** * 获取页号 * * @return */ public int getCurrentPage() { if (intCurrentPage > getPageTotal()) { intCurrentPage = getPageTotal(); } return intCurrentPage > 0 ? intCurrentPage : 0; } /** * 查询条件串 * * @return */ public String getSearchString() { return searchString; } public void setSearchString(String searchString) { if (searchString != null && !searchString.equals("")) { this.searchString = searchString; } if (searchString.indexOf("&") != 0) { this.searchString = "&" + searchString; } } /* * 计算有关分页参数 */ private void init() { } /** * 下一页的页号 */ public long getNextPage() { if (getCurrentPage() + 1 <= getPageTotal()) return getCurrentPage() + 1; else return -1; } /** * 上一页的页号 */ public long getPreviouPage() { if (getCurrentPage() - 1 > 0) return getCurrentPage() - 1; else return -1; } /** * 首页的页号 */ public int getBeginPage() { if (getCurrentPage() > 1) return 1; else return -1; } /** * 未页的页号 */ public long getEndPage() { if (getCurrentPage() < getPageTotal()) return getPageTotal(); else return -1; } /** * 设置查询分页 */ public void setQueryPage(String pageno, int totalRecord) { if (pageno != null && !pageno.equals("")) { this.intCurrentPage = Integer.valueOf(pageno).intValue(); } setTotal(totalRecord); init(); } public void setQueryPage(int totalRecord) { setTotal(totalRecord); init(); } public void setQueryPage(Query query, int totalRecord) { setTotal(totalRecord); init(); if (getPageSize() > 0) { // 设置分页起始记录号 query .setFirstResult((int) ((getCurrentPage() - 1) * getPageSize())); // 设置页内数据量 query.setMaxResults((int) getPageSize()); } } public String getPageInfo(String url, String searchStr) { int idx = url.indexOf("?"); if (idx != -1) { String param = url.substring(idx + 1); url = url.substring(0, idx); searchStr = param + "&" + searchStr; } setSearchString(searchStr); return getPageInfo(url); } public String getPageInfoButton(String url, String searchStr) { int idx = url.indexOf("?"); if (idx != -1) { String param = url.substring(idx + 1); url = url.substring(0, idx); searchStr = param + "&" + searchStr; } setSearchString(searchStr); return getPageInfoButton(url); } /** * 分页的字符串 首页 上一页 下一页 未页 */ public String getPageInfo(String url) { this.init(); String strPageInfo = ""; long intStart, intEnd; if (getTotal() <= 0 || url.equals("")) return ""; if (getBeginPage() != -1) strPageInfo += "<a href=" + url + "?iPage=1" + getSearchString() + " class='grey12'>首页</a> "; if (getPreviouPage() != -1) strPageInfo += "<a href=" + url + "?iPage=" + getPreviouPage() + getSearchString() + " class='grey12'>上一页</a> "; // 设置开始 if (getCurrentPage() - 2 > 0) { intStart = getCurrentPage() - 2; } else intStart = 1; if (intStart + 7 > getPageTotal()) intStart = getPageTotal() - 7; if (intStart <= 0) intStart = 1; // 设置结束 if (intStart + 7 <= getPageTotal()) intEnd = intStart + 7; else intEnd = getPageTotal(); // 形式: ... 1 2 3 4 5 ... for (long i = intStart; i <= intEnd; i++) { if (getCurrentPage() == i) strPageInfo += "<font color=red class='font_blue12'>" + i + "</font> "; else strPageInfo += "<a href=" + url + "?iPage=" + i + getSearchString() + " class='blue12'>" + "" + i + " " + "</a> "; } if (getNextPage() != -1) strPageInfo += "<a href=" + url + "?iPage=" + getNextPage() + getSearchString() + " class='grey12'>下一页</a> "; if (getEndPage() != -1) strPageInfo += "<a href=" + url + "?iPage=" + getEndPage() + getSearchString() + " class='grey12'>末页</a>"; strPageInfo += "<span class='font_blue12'><img width=1 height=1 hspace=6>记录数:<font color=red>" + getTotal() + "</font>, 页次:<font color=red>"; strPageInfo += getCurrentPage() + "</font>/" + getPageTotal() + "页.</span>"; return strPageInfo; } /** * 分页的字符串 首页 上一页 下一页 未页 */ public String getPageInfoButton(String url) { this.init(); String strPageInfo = ""; long intStart, intEnd; if (getTotal() <= 0 || url.equals("")) return ""; if (getBeginPage() != -1) strPageInfo += "<a href=" + url + "?iPage=1" + getSearchString() + "><img src=\"/front/images/info/all_questions_bottom_1.gif\" width=\"17\" height=\"17\" border=\"0\" /></a> "; if (getPreviouPage() != -1) strPageInfo += "<a href=" + url + "?iPage=" + getPreviouPage() + getSearchString() + "><img src=\"/front/images/info/all_questions_bottom_2.gif\" width=\"52\" height=\"17\" border=\"0\" /></a> "; // 设置开始 if (getCurrentPage() - 2 > 0) { intStart = getCurrentPage() - 2; } else intStart = 1; if (intStart + 7 > getPageTotal()) intStart = getPageTotal() - 7; if (intStart <= 0) intStart = 1; // 设置结束 if (intStart + 7 <= getPageTotal()) intEnd = intStart + 7; else intEnd = getPageTotal(); // 形式: ... 1 2 3 4 5 ... for (long i = intStart; i <= intEnd; i++) { if (getCurrentPage() == i) { } // strPageInfo += "<font color=red size=3> " + i + // " </font> "; else strPageInfo += "<a href=" + url + "?iPage=" + i + getSearchString() + ">" + "<font size=2> " + i + " </font> " + "</a> "; } if (getNextPage() != -1) strPageInfo += "<a href=" + url + "?iPage=" + getNextPage() + getSearchString() + "><img src=\"/front/images/info/all_questions_bottom_3.gif\" width=\"52\" height=\"17\" border=\"0\" /></a> "; if (getEndPage() != -1) strPageInfo += "<a href=" + url + "?iPage=" + getEndPage() + getSearchString() + "><img src=\"/front/images/info/all_questions_bottom_4.gif\" width=\"17\" height=\"17\" border=\"0\" /></a>"; strPageInfo += "<img width=1 height=1 hspace=6>记录数:<font color=red>" + getTotal() + "</font>, 页次:<font color=red>"; strPageInfo += getCurrentPage() + "</font>/" + getPageTotal() + "页."; return strPageInfo; } /** * 分页的字符串 首页 上一页 下一页 未页 */ public String getPageInfo(HttpServletRequest request) { this.init(); StringBuffer url = request.getRequestURL(); StringBuffer paraStr = new StringBuffer(); Enumeration paramNames = request.getParameterNames(); // request. while (paramNames.hasMoreElements()) { String paramName = (String) paramNames.nextElement(); try { paramName = URLEncoder.encode(new String(paramName .getBytes("8859_1"), "UTF-8")); // if(StringUtil.equalsIgnoreCase(paramName,"method")) { // //如果已有method方法,则跳过 // if(StringUtil.isNotEmpty(this.getMethod())) { // paraStr.append("&"); // paraStr.append(paramName); // paraStr.append("="); // paraStr.append(this.getMethod()); // continue; // } // } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if (!paramName.equals("iPage")) { String paramValue = request.getParameter(paramName).replace( '<', '(').replace('>', ')'); // if (StringUtil.equalsIgnoreCase(paramName,"method")) { // paramValue = (String)request.getAttribute("method"); // } if (paramValue == null) { paramValue = ""; } if (!paramName.equals("content")) { paraStr.append("&"); paraStr.append(paramName); paraStr.append("="); paraStr.append(paramValue); } } } if (paraStr.length() > 0) { return getPageInfo(url.toString(), paraStr.toString()); } else { return getPageInfo(url.toString()); } } public String getPageInfoForCheckBox(HttpServletRequest request) { this.init(); StringBuffer url = request.getRequestURL(); StringBuffer paraStr = new StringBuffer(); Enumeration paramNames = request.getParameterNames(); // request. while (paramNames.hasMoreElements()) { String paramName = (String) paramNames.nextElement(); try { paramName = URLEncoder.encode(new String(paramName .getBytes("8859_1"), "UTF-8")); // if(StringUtil.equalsIgnoreCase(paramName,"method")) { // //如果已有method方法,则跳过 // if(StringUtil.isNotEmpty(this.getMethod())) { // paraStr.append("&"); // paraStr.append(paramName); // paraStr.append("="); // paraStr.append(this.getMethod()); // continue; // } // } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if (!paramName.equals("iPage")) { String paramValue = request.getParameter(paramName).replace( '<', '(').replace('>', ')'); // if (StringUtil.equalsIgnoreCase(paramName,"method")) { // paramValue = (String)request.getAttribute("method"); // } if (paramValue == null) { paramValue = ""; } if (!paramName.equals("content")) { paraStr.append("&"); paraStr.append(paramName); paraStr.append("="); paraStr.append(paramValue); } if (paramName.equalsIgnoreCase("checkbox")) { String[] paramValues = request .getParameterValues(paramName); for (int i = 0; i < paramValues.length; i++) { paraStr.append("&"); paraStr.append(paramName); paraStr.append("="); paraStr.append(paramValues[i]); } } } } if (paraStr.length() > 0) { return getPageInfo(url.toString(), paraStr.toString()); } else { return getPageInfo(url.toString()); } } /** * 分页的字符串 首页 上一页 下一页 未页 */ public String getPageInfo(String method, HttpServletRequest request) { this.init(); StringBuffer url = request.getRequestURL(); StringBuffer paraStr = new StringBuffer(); Enumeration paramNames = request.getParameterNames(); while (paramNames.hasMoreElements()) { String paramName = (String) paramNames.nextElement(); try { paramName = URLEncoder.encode(new String(paramName .getBytes("8859_1"), "UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if (!paramName.equals("iPage")) { String paramValue = request.getParameter(paramName).replace( '<', '(').replace('>', ')'); if (paramValue == null) { paramValue = ""; } if (!paramName.equals("content")) { if (paramName.equals("method")) { paraStr.append("&"); paraStr.append(paramName); paraStr.append("="); paraStr.append(method); } else { paraStr.append("&"); paraStr.append(paramName); paraStr.append("="); paraStr.append(paramValue); } } } } if (paraStr.length() > 0) { return getPageInfo(url.toString(), paraStr.toString()); } else { return getPageInfo(url.toString()); } } /** * 分页的字符串 首页 上一页 下一页 未页 */ public String getPageInfoForBiostimeNew(HttpServletRequest request, boolean isAttribute) { this.init(); StringBuffer url = request.getRequestURL(); StringBuffer paraStr = new StringBuffer(); Enumeration paramNames = request.getParameterNames(); Enumeration attributeNames = request.getAttributeNames(); while (paramNames.hasMoreElements()) { String paramName = (String) paramNames.nextElement(); try { paramName = URLEncoder.encode(new String(paramName .getBytes("8859_1"), "UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if (!paramName.equals("iPage")) { String paramValue = request.getParameter(paramName).replace( '<', '(').replace('>', ')'); if (paramValue == null) { paramValue = ""; } if (paramName.equals("method") || paramName.equals("moduleCode")) { paraStr.append("&"); paraStr.append(paramName); paraStr.append("="); paraStr.append(paramValue); } } } if (isAttribute == true) { while (attributeNames.hasMoreElements()) { String attributeName = (String) attributeNames.nextElement(); if (StringUtils.isNotEmpty(attributeName) && StringUtils.equalsIgnoreCase(attributeName, "moduleCode")) { String attributeValue = (String) request .getAttribute(attributeName); paraStr.append("&"); paraStr.append(attributeName); paraStr.append("="); paraStr.append(attributeValue); } } } if (paraStr.length() > 0) { return getPageInfo(url.toString(), paraStr.toString()); } else { return getPageInfo(url.toString()); } } /** * 分页的字符串 首页 上一页 下一页 未页,应用于Hyt后台 */ public String getPageInfoForHyt(HttpServletRequest request, boolean encode, List<String> stringList) { this.init(); StringBuffer url = request.getRequestURL(); StringBuffer paraStr = new StringBuffer(); Enumeration paramNames = request.getParameterNames(); Enumeration attributeNames = request.getAttributeNames(); while (paramNames.hasMoreElements()) { String paramName = (String) paramNames.nextElement(); try { paramName = URLEncoder.encode(new String(paramName .getBytes("8859_1"), "UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if (!paramName.equals("iPage")) { String paramValue = request.getParameter(paramName).replace( '<', '(').replace('>', ')'); if (paramValue == null) { paramValue = ""; } if (paramName.equals("method") || paramName.equals("moduleCode")) { paraStr.append("&"); paraStr.append(paramName); paraStr.append("="); paraStr.append(paramValue); } if (encode == true) { if (stringList != null) { if (stringList.contains(paramName)) { try { paramValue = URLEncoder.encode(paramValue, "UTF-8"); paraStr.append("&"); paraStr.append(paramName); paraStr.append("="); paraStr.append(paramValue); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } } } } if (paraStr.length() > 0) { return getPageInfo(url.toString(), paraStr.toString()); } else { return getPageInfo(url.toString()); } } /** * 分页的字符串 首页 上一页 下一页 未页 */ public String getPageInfo(HttpServletRequest request, Boolean encode) { this.init(); StringBuffer url = request.getRequestURL(); StringBuffer paraStr = new StringBuffer(); Enumeration paramNames = request.getParameterNames(); while (paramNames.hasMoreElements()) { String paramName = (String) paramNames.nextElement(); try { paramName = URLEncoder.encode(new String(paramName .getBytes("8859_1"), "UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if (!paramName.equals("iPage")) { String paramValue = request.getParameter(paramName).replace( '<', '(').replace('>', ')'); if (encode) { try { paramValue = URLEncoder.encode(paramValue, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } /* * try { if(iPage == null) paramValue = URLEncoder.encode(new * String(paramValue.getBytes("8859_1"),"GBK")); else paramValue * = URLEncoder.encode(new * String(paramValue.getBytes("8859_1"),"UTF-8")); if(iPage != * null) paramValue = URLEncoder.encode(new * String(paramValue.getBytes("8859_1"),"UTF-8")); } * catch(UnsupportedEncodingException e) { e.printStackTrace(); * } */ if (paramValue == null) { paramValue = ""; } if (!paramName.equals("content")) { paraStr.append("&"); paraStr.append(paramName); paraStr.append("="); paraStr.append(paramValue); } } } if (paraStr.length() > 0) { return getPageInfo(url.toString(), paraStr.toString()); } else { return getPageInfo(url.toString()); } } /** * 分页的字符串 首页 上一页 下一页 未页 前台有图片的 */ public String getFrontPageInfo(HttpServletRequest request) { this.init(); StringBuffer url = request.getRequestURL(); StringBuffer paraStr = new StringBuffer(); Enumeration paramNames = request.getParameterNames(); while (paramNames.hasMoreElements()) { String paramName = (String) paramNames.nextElement(); try { paramName = URLEncoder.encode(new String(paramName .getBytes("8859_1"), "UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if (!paramName.equals("iPage")) { String paramValue = request.getParameter(paramName).replace( '<', '(').replace('>', ')'); String iPage = request.getParameter("iPage"); /* * try { if(iPage == null) paramValue = URLEncoder.encode(new * String(paramValue.getBytes("8859_1"),"GBK")); else paramValue * = URLEncoder.encode(new * String(paramValue.getBytes("8859_1"),"UTF-8")); if(iPage != * null) paramValue = URLEncoder.encode(new * String(paramValue.getBytes("8859_1"),"UTF-8")); } catch * (UnsupportedEncodingException e) { e.printStackTrace(); } */ if (paramValue == null) { paramValue = ""; } if (!paramName.equals("content")) { paraStr.append("&"); paraStr.append(paramName); paraStr.append("="); paraStr.append(paramValue); System.out.println(paramValue); } } } if (paraStr.length() > 0) { return getPageInfoButton(url.toString(), paraStr.toString()); } else { return getPageInfoButton(url.toString()); } } /** * 分页的字符串 首页 上一页 下一页 未页,应用于会员通后台 */ public String getPageInfoForHyt(HttpServletRequest request, boolean isAttribute) { this.init(); StringBuffer url = request.getRequestURL(); StringBuffer paraStr = new StringBuffer(); Enumeration paramNames = request.getParameterNames(); Enumeration attributeNames = request.getAttributeNames(); while (paramNames.hasMoreElements()) { String paramName = (String) paramNames.nextElement(); try { paramName = URLEncoder.encode(new String(paramName .getBytes("8859_1"), "UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if (!paramName.equals("iPage")) { String paramValue = request.getParameter(paramName).replace( '<', '(').replace('>', ')'); if (paramValue == null) { paramValue = ""; } if (paramName.equals("method") || paramName.equals("moduleCode")) { paraStr.append("&"); paraStr.append(paramName); paraStr.append("="); paraStr.append(paramValue); } } } if (isAttribute == true) { while (attributeNames.hasMoreElements()) { String attributeName = (String) attributeNames.nextElement(); if (StringUtils.isNotEmpty(attributeName) && StringUtils.equalsIgnoreCase(attributeName, "moduleCode")) { String attributeValue = (String) request .getAttribute(attributeName); paraStr.append("&"); paraStr.append(attributeName); paraStr.append("="); paraStr.append(attributeValue); } } } if (paraStr.length() > 0) { return getPageInfo(url.toString(), paraStr.toString()); } else { return getPageInfo(url.toString()); } } /** * 生成静态页面的链接. * * @param url * 文件链接,文件名不带后缀 * @return */ public String getPageButton(String url) { this.init(); StringBuffer buf = new StringBuffer(); long intStart, intEnd; int pageLength = 9; if (getTotal() <= 0 || url.equals("")) return ""; if (getBeginPage() == -1) buf.append("<font color=999999 face=webdings>9</font> "); else buf.append("<a href=" + url + ".html><font face=webdings>9</font></a> "); if (getPreviouPage() == -1) buf.append("<font color=999999 face=webdings>3</font> "); else buf.append("<a href=" + url + "-p" + getPreviouPage() + ".html><font face=webdings>3</font></a> "); // 设置开始 if (getCurrentPage() - 2 > 0) intStart = getCurrentPage() - 2; else intStart = 1; if (intStart + pageLength > getPageTotal()) intStart = getPageTotal() - pageLength; if (intStart <= 0) intStart = 1; // 设置结束 if (intStart + pageLength <= getPageTotal()) intEnd = intStart + pageLength; else intEnd = getPageTotal(); // 形式: ... 1 2 3 4 5 ... for (long i = intStart; i <= intEnd; i++) { if (getCurrentPage() == i) buf.append("<font color=red>" + i + "</font> "); else if (i == 1) buf.append("<a href=" + url + ".html>" + i + "</a> "); else buf .append("<a href=" + url + "-p" + i + ".html>" + i + "</a> "); } if (getNextPage() == -1) buf.append("<font color=999999 face=webdings>4</font> "); else buf.append("<a href=" + url + "-p" + getNextPage() + ".html><font face=webdings>4</font></a> "); if (getEndPage() == -1) buf.append("<font color=999999 face=webdings>:</font>"); else buf.append("<a href=" + url + "-p" + getEndPage() + ".html><font face=webdings>:</font></a>"); return buf.toString(); } /** * 共7004 条信息,当前显示第 1 - 40 条,共 176 页 * * @return */ public String getPageInfo() { this.init(); StringBuffer buf = new StringBuffer(); buf.append("共 <strong>" + getTotal() + "</strong> 条信息,"); long startPosition = this.getPosition(); if (getTotal() <= 0) startPosition = 0; long endPosition = startPosition + getPageSize(); if (endPosition > getTotal()) endPosition = getTotal(); buf.append("当前显示第 <strong>" + startPosition + "</strong> - <strong>" + endPosition + "</strong> 条,"); buf.append("共 <strong>" + getPageTotal() + "</strong> 页"); return buf.toString(); } public long getIntTotal() { return intTotal; } public void setIntTotal(int intTotal) { this.intTotal = intTotal; } public String getMethod() { return method; } public void setMethod(String method) { this.method = method; } public Object getPageInfoForHyt(HttpServletRequest request, boolean isAttribute, StringBuffer custom_paraStr) { this.init(); StringBuffer url = request.getRequestURL(); StringBuffer paraStr = new StringBuffer(); Enumeration paramNames = request.getParameterNames(); Enumeration attributeNames =request.getAttributeNames(); while (paramNames.hasMoreElements()) { String paramName = (String) paramNames.nextElement(); try { paramName = URLEncoder.encode(new String(paramName.getBytes("8859_1"), "UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if (!paramName.equals("iPage")) { String paramValue = request.getParameter(paramName).replace('<', '(').replace('>', ')'); if (paramValue == null) { paramValue = ""; } if (paramName.equals("method")||paramName.equals("moduleCode")) { paraStr.append("&"); paraStr.append(paramName); paraStr.append("="); paraStr.append(paramValue); } } } if(isAttribute==true) { while(attributeNames.hasMoreElements()) { String attributeName=(String)attributeNames.nextElement(); if(StringUtils.isNotEmpty(attributeName)&&StringUtils.equalsIgnoreCase(attributeName, "moduleCode")) { String attributeValue=(String)request.getAttribute(attributeName); paraStr.append("&"); paraStr.append(attributeName); paraStr.append("="); paraStr.append(attributeValue); } } } paraStr.append(custom_paraStr); if (paraStr.length() > 0) { return getPageInfo(url.toString(), paraStr.toString()); } else { return getPageInfo(url.toString()); } } }
//分页 private Pager getPager(HttpServletRequest request) { String iPage = request.getParameter("iPage"); if (StringUtil.isEmpty(iPage)) iPage = "1"; Pager pager = new Pager(); pager.setCurrentPage(iPage); return pager; }