JSP自定义分页标签
来自: http://my.oschina.net/chengxiaoyuan/blog/614989
分页实现类,继承TagSupport,doStartTag方法内拼凑分页的html。
import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; public class FromPageTag extends TagSupport { private static final long serialVersionUID = -3662095604913922414L; private int pageNo;//页码 private int pageSize;//每页数量 private int totalRecord;//总记录数 @Override public int doStartTag() throws JspException { int tag = totalRecord % pageSize; if (tag > 0) { tag = 1; } int endPageNo = totalRecord / pageSize + tag; StringBuilder sb = new StringBuilder( "<div class=\"dataTables_wrapper no-footer\"><div class=\"dataTables_paginate paging_simple_numbers\">"); // 前一页 sb.append("<a class=\"paginate_button previous disabled\" "); if (pageNo > 1) { sb.append("onclick=\"_turnPage(").append(pageNo - 1).append(")\""); } sb.append(">上一页</a>"); // 第一页 sb.append(addPageCell(1)); if (endPageNo > 1) { sb.append(addPageCell(2)); } if (endPageNo > 2) { sb.append(addPageCell(3)); } if (pageNo > 5) { sb.append("<span>…</span>"); } // 中间页码 for (int i = pageNo - 1; i <= pageNo + 1; i++) { if (i <= 3) { continue; } if (i >= endPageNo - 2) { continue; } sb.append(addPageCell(i)); } if (pageNo < endPageNo - 3) { sb.append("<span>…</span>"); } //后面页码 if (endPageNo - 2 > 3) { sb.append(addPageCell(endPageNo - 2)); } if (endPageNo - 1 > 3) { sb.append(addPageCell(endPageNo - 1)); } if(endPageNo > 3) { sb.append(addPageCell(endPageNo)); } //最后一页 sb.append("<a class=\"paginate_button next disabled\""); if(pageNo < endPageNo) { sb.append("onclick=\"_turnPage(").append(pageNo+1).append(")\"");; } sb.append(">下一页</a></div>"); try { if (sb != null) { JspWriter out = pageContext.getOut(); out.print(sb); } } catch (Exception e) { throw new JspException("错误"); } return super.doStartTag(); } private String addPageCell(int number) { StringBuilder sb = new StringBuilder(); sb.append("<span><a class=\"paginate_button "); if (pageNo != number) { sb.append("\" onclick=\"_turnPage(").append(number).append(")\""); } else { sb.append(" current\""); } sb.append(">").append(number).append("</a></span>"); return sb.toString(); } public int getTotalRecord() { return totalRecord; } public void setTotalRecord(int totalRecord) { this.totalRecord = totalRecord; } public int getPageNo() { return pageNo; } public void setPageNo(int pageNo) { this.pageNo = pageNo; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } }
2. 在WEB-INF/tlds内增加 formPage.tld
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version>1.0</tlib-version><!-- 标签库的版本 --> <jsp-version>1.2</jsp-version><!-- 这个标签库要求的JSP规范版本 --> <short-name>mytag</short-name><!-- JSP页面编写工具可以用来创建助记名的可选名字 --> <tag> <name>formPage</name><!-- 唯一标签名 --> <tag-class>com.common.FromPageTag</tag-class><!--包路径自己定--> <body-content>empty</body-content><!-- 正文内容类型 --> <!-- 参数 --> <attribute> <name>pageNo</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>pageSize</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>totalRecord</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
3. web.xml中定义
<jsp-config> <taglib> <taglib-uri>/tld/page</taglib-uri> <taglib-location>/WEB-INF/tlds/formPage.tld</taglib-location> </taglib> </jsp-config>
4. 使用
<%@ taglib uri="/tld/page" prefix="myTag" %> <myTag:formPage pageSize="${pageSize }" pageNo="${pageNo }" totalRecord="${totalRecord }"/> <script type="text/javascript"> function _turnPage(pageNo) { document.getElementById("pageNo").value=pageNo; document.getElementById("pageSize").value=20; document.getElementById("myForm").submit(); } </script>
本文由用户 sdsd55 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!