浏览 2235 次
锁定老帖子 主题:模拟struts 论坛 分页
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-10-19
最后修改:2010-10-19
参照网上修改了自己的分页代码: 最终效果: 分页工具类: public class Pager<T> implements Serializable { private int pageNo = 1; private int pageSize = Constants.DEFAULT_PAGE_SIZE; private static final int LISTSTEP = 6; private String orderBy = null; private Order order = Order.ASC; private List<T> result = null; private int totalCount = -1; // 构造函数 public Pager() { } public Pager(final int pageNo) { setPageNo(pageNo); } public Pager(final int pageNo, final int pageSize) { setPageNo(pageNo); setPageSize(pageSize); } // 查询参数相关函数 public int getPageNo() { return pageNo; } public void setPageNo(final int pageNo) { if(pageNo<1) this.pageNo = 1; else this.pageNo = pageNo; } public int getPageSize() { return pageSize; } public void setPageSize(final int pageSize) { if(pageSize<1) this.pageSize = Constants.DEFAULT_PAGE_SIZE; else this.pageSize = pageSize; } public boolean isPageSizeSetted() { // return pageSize > 0; if (pageSize < 1) { pageSize = Constants.DEFAULT_PAGE_SIZE; } return true; } public String getOrderBy() { return orderBy; } public void setOrderBy(final String orderBy) { this.orderBy = orderBy; } public boolean isOrderBySetted() { return StringUtils.isNotBlank(orderBy); } public Order getOrder() { return order; } public void setOrder(final Order order) { if (Order.ASC.equals(order) || Order.DESC.equals(order)) { this.order = order; } else throw new IllegalArgumentException("Order should be 'desc' or 'asc'"); } /** * 根据pageNo和pageSize计算当前页第一条记录在总结果集中的索引位置. */ public int getFirstIndex() { if (pageNo < 1 || pageSize < 1) return 0; else return ((pageNo - 1) * pageSize) < totalCount-1? ((pageNo - 1) * pageSize) : totalCount-1; } /** * 是否已设置第一条记录记录在总结果集中的位置. */ public boolean isFirstIndexSetted() { return true; } /** * 取得页内的记录列数. */ public List<T> getResult() { return result; } public void setResult(final List<T> result) { this.result = result; } /** * 取得总记录数. */ public int getTotalCount() { return totalCount; } public void setTotalCount(final int totalCount) { this.totalCount = totalCount; } /** * 计算总页数. */ public int getTotalPages() { if (totalCount < 1) return 0; return (totalCount + pageSize - 1) / pageSize; } /** * 是否还有下一页. */ public boolean isHasNext() { return (pageNo + 1 <= getTotalPages()); } /** * 取得下一页 */ public int getNextPage() { if (isHasNext()) return pageNo + 1; else return pageNo; } /** * 是否还有上一页. */ public boolean isHasPre() { System.out.println(pageNo+",.,.,."); return (pageNo - 1 >= 1); } /** * 取得上一页. */ public int getPrePage() { if (isHasPre()) return pageNo - 1; else return pageNo; } /** * 获取页码 */ public List<Integer> getPageNumbers() { int totalPages = getTotalPages(); int startPageNo = 1; int currentPageNo = pageNo; int endPageNo = totalPages; List<Integer> pageNumbers = new ArrayList<Integer>(); // 1 2 3 4 5 6 7 8 if(endPageNo<=LISTSTEP+2){ for(int i=0; startPageNo<=endPageNo; startPageNo++,i++) { pageNumbers.add(startPageNo); } }else{ // 1,2,3,4,5,6,...,pagecount-1,pagecount if(currentPageNo < LISTSTEP){ for (int i = 1; i <= LISTSTEP; i++) { pageNumbers.add(i); } pageNumbers.add(-1); // -1 表示之间有有...间隔 pageNumbers.add(endPageNo-1); pageNumbers.add(endPageNo) ; } else if (currentPageNo > endPageNo - LISTSTEP + 1) { // 右边 // 1,2,...pagecount-5,pagecount-4,pagecount-3,pagecount-2,pagecount-1,pagecount pageNumbers.add(1); pageNumbers.add(2); pageNumbers.add(-1); for (int i = endPageNo - LISTSTEP +1; i <= endPageNo; i++) { pageNumbers.add(i); } }else { // 中间 // 1,2,...,curpage-1,curpage,curpage+1,...,pagecount-1,pagecount pageNumbers.add(1); pageNumbers.add(2); pageNumbers.add(-1); int offset = (LISTSTEP - 4) / 2; for (int i = currentPageNo - offset; i <= currentPageNo + offset; i++) { pageNumbers.add(i); } pageNumbers.add(-1); pageNumbers.add(endPageNo-1); pageNumbers.add(endPageNo) ; } } return pageNumbers; } } 分页标签类: public class PagerTag extends BodyTagSupport{ private static final long serialVersionUID = 9161072674434807502L; public static final int DEFAULT_PAGE_STEP = 5; private String url; private Pager pager; public void setUrl(String url) { this.url = url; } public void setPager(Pager pager) { this.pager = pager; } @Override public int doEndTag() throws JspException { if (pager == null || pager.getTotalCount() == 0) { try { pageContext.getOut().println("<center></center>"); } catch (IOException e) { throw new JspException(e); } return EVAL_PAGE; } int pageNo = pager.getPageNo(); int totalPages = pager.getTotalPages(); StringBuilder sb = new StringBuilder(); sb.append("<form name='pageController' id='pageController' action='' method='post'>\r\n").append( (new StringBuilder("<input type='hidden' id='pageNo' name='pageNo' value='")).append(pageNo).append( "' />\r\n").toString()); HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); Enumeration enumeration = request.getParameterNames(); String name = null; String value = null; while (enumeration.hasMoreElements()) { name = (String) enumeration.nextElement(); value = request.getParameter(name); if (name.equals("pageNo")) { if (value != null && !"".equals(value)) pageNo = Integer.parseInt(value); pager.setPageNo(pageNo); } else if(name.indexOf("content")==-1) { sb.append("<input type='hidden' name='").append(name).append("' value='").append(value).append( "'/>\r\n"); } } // sb.append((new StringBuilder("<span class='zfd yy5'>当前第")).append(pageNo).toString()).append("/").append(totalPages).append( // "页 </span>").toString(); if (pager.isHasPre()) { //sb.append("<a href='javascript:turnToPage(1)' title='首页' class='prev_page'>首页</a>\r\n"); sb.append("<a href='javascript:turnToPage(").append(pager.getPrePage()).append( ")' title='上一页' class='prev_page'>« 上一页</a>\r\n"); } else { //sb.append("<span class='disabled prev_page'>首页</span>"); sb.append("<span class='disabled prev_page'>« 上一页</span>\r\n"); } // 处理pageNum List<Integer> pageNums = pager.getPageNumbers(); for (int i = 0; i < pageNums.size(); i++) { if (pageNums.get(i) == pageNo) { sb.append("<span class='current'>"+ pageNums.get(i) + "</span> \r\n"); }else if(pageNums.get(i)==-1){ sb.append("<span > … </span>"); }else { sb.append("<a href='javascript:turnToPage(").append(pageNums.get(i)).append( ")'>" + pageNums.get(i) + "</a> \r\n"); } } if (pager.isHasNext()) { sb.append("<a href='javascript:turnToPage(").append(pager.getNextPage()).append( ")' title='下一页' >下一页 »</a>\r\n"); //sb.append("<a href='javascript:turnToPage(").append(totalPages).append(")' title='尾页' class='asxye-2 zfd yy5'>尾页</a>\r\n"); } else { sb.append("<span class='disabled next_page'>下一页 »</span>\r\n"); //sb.append("<span class='disabled next_page'>尾页</span>\r\n"); } /* sb.append("<span class='zfd yy5'>跳转到 <select onChange='turnToPage(this.value)'>\r\n"); for (int i = 1; i <= totalPages; i++) if (i == pageNo) sb.append(" <option value='").append(i).append("' selected='selected'>\u7B2C").append(i).append( "页</option>\r\n"); else sb.append(" <option value='").append(i).append("'>第").append(i).append("页</option>\r\n"); sb.append("</select></span>\r\n"); */ // 输入方式的跳转 // sb.append(" goto <input type='text' // onChange='turnToPage(this.value)'>\r\n"); sb.append("</form>\r\n"); // 翻页函数 sb.append("<script language='javascript'>\r\n"); sb.append(" function turnToPage(pageNo){\r\n"); sb.append(" var form = document.pageController;\r\n"); sb.append(" if(pageNo").append(">").append(totalPages).append(") {\r\n"); sb.append(" pageNo=").append(totalPages).append(";\r\n"); sb.append(" }\r\n"); sb.append(" if(pageNo").append("< 1){\r\n"); sb.append(" pageNo=1;\r\n"); sb.append(" }\r\n"); sb.append(" form.").append("pageNo").append(".value=pageNo;\r\n"); sb.append(" form.action='").append(url).append("';\r\n"); sb.append(" form.submit();\r\n"); sb.append(" }\r\n"); sb.append("</script>\r\n"); try { pageContext.getOut().println(sb.toString()); } catch (IOException e) { e.printStackTrace(); } return EVAL_PAGE; } @Override public int doAfterBody() throws JspException { try { this.getBodyContent().writeOut(getPreviousOut()); } catch (IOException e) { throw new JspException(e); } return SKIP_BODY; } } 然后配个分页标签 <?xml version="1.0" encoding="UTF-8"?> <taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"> <tlib-version>2.0</tlib-version> <short-name>hw</short-name> <uri>http://www.myframe.com/pager</uri> <tag> <description>分页标签</description> <name>pager</name> <tag-class>com.labixiaoxin.myframe.web.tag.PagerTag</tag-class> <body-content>JSP</body-content> <attribute> <name>pager</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.Object</type> </attribute> <attribute> <name>url</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |