JSP自定义分页标签TAG全过程

首先我们需要在WEB-INF/tld/目录下创建page.tld文件
<?xml version="1.0" encoding="ISO-8859-1"?> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>page</short-name> <uri></uri> <tag> <name>htmlPage</name> <tag-class>com.lss.common.tag.PageTag</tag-class> <body-content>JSP</body-content> <description>htmlPage Tag</description> <attribute> <name>pageNo</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>pageSize</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>totalSum</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>url</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>showPage</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>

package com.lss.common.tag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; import com.lss.common.page.HtmlPage; import com.lss.common.page.Page; import com.lss.common.page.VariablePage; /** * 分页处理标签 * * @description * @DATE 2012-3-11下午09:08:46 */ public class PageTag extends TagSupport { private int pageNo; private int pageSize = 10; private int totalSum; private int showPage = 10; private String url; 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; } public int getTotalSum() { return totalSum; } public void setTotalSum(int totalSum) { this.totalSum = totalSum; } public int getShowPage() { return showPage; } public void setShowPage(int showPage) { this.showPage = showPage; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } @Override public int doEndTag() throws JspException { if (pageSize == 0) { return TagSupport.SKIP_PAGE;// 不显示分页 } else if (pageSize > totalSum) { return TagSupport.SKIP_BODY;// 不显示分页 } JspWriter out = pageContext.getOut(); try { if (url.indexOf(";jsessionid=") == -1) { if (url.indexOf("?") == -1) { url += ";jsessionid=" + pageContext.getSession().getId() + "?pageNo="; } else { url = url.replaceFirst("\\?", ";jsessionid=" + pageContext.getSession().getId() + "?"); if (url.indexOf("pageNo=") == -1) { url += "&pageNo="; } } } url = pageContext.getServletContext().getContextPath() + url; VariablePage variablePage = new VariablePage(pageNo, totalSum, pageSize, url); variablePage.setShowPageNum(showPage); Page page = new HtmlPage(variablePage); out.print(page.getFirstNo() + " " + page.getBackpageNum() + " " + page.pagination() + " " + page.getNextpageNum() + " " + page.getLastNo()); } catch (IOException ex) { ex.printStackTrace(); } return super.doEndTag(); } @Override public void release() { url = null; pageNo = 0; totalSum = 0; pageSize = 10; super.release(); } private static final long serialVersionUID = -2642907859641024483L; }

/** * HTML 分页类 * * @description * @DATE 2012-3-11下午10:33:14 */ public class HtmlPage extends Page { public HtmlPage(VariablePage variablePage) { super(variablePage); } public String pagination() { String printNo = ""; // 如果已分页;并且页数小于等于要显示的页数 if (variablePage.totalPage > 1 && variablePage.totalPage <= variablePage.showPageNum) { return displayAll(); // 如果分页数:大于显示的页码数 } else if (variablePage.totalPage > 1 && variablePage.totalPage > variablePage.showPageNum) { if (variablePage.pageNo == 1) {// 当前页等于第一页 return fromFirstPagePrint(); } else if (variablePage.pageNo == variablePage.totalPage) {// 当前页等于最后一页 return fromLastPagePrint(); } else {// 如果当前页:即不是首页也不是尾页 if (variablePage.showPageNum % 2 == 0) {// 可以平分页码 int print$No = variablePage.showPageNum / 2; if (variablePage.pageNo >= print$No) { int index$No = variablePage.pageNo - print$No; if (variablePage.pageNo + print$No >= variablePage.totalPage) { return fromLastPagePrint(); } else { if (index$No == 0) index$No = 1; for (int i = index$No; i < (variablePage.showPageNum + index$No); i++) { if (i == variablePage.pageNo) {// 如果是当前页:不添加连接URL printNo += (i + variablePage.split); } else { printNo += (buildA(variablePage, i) + variablePage.split); } } } } else { return fromFirstPagePrint(); } } else {// 打印页数不是偶数时: int print$No = variablePage.showPageNum / 2 + 1; if (variablePage.pageNo >= print$No && variablePage.pageNo + print$No < variablePage.totalPage) { int index$No = variablePage.pageNo - print$No + 1; for (int i = index$No; i < variablePage.showPageNum + index$No; i++) { if (i == variablePage.pageNo) {// 如果是当前页:不添加连接URL printNo += (i + variablePage.split); } else { printNo += (buildA(variablePage, i) + variablePage.split); } } } else if (variablePage.pageNo <= print$No) {// 从第一页开始 return fromFirstPagePrint(); } else { return fromLastPagePrint(); } } } return (printNo); } else { return "1"; } } public String getBackpageNum() { if (variablePage.pageNo <= 1) { return buildSpan("上一页", variablePage); } else { return buildA("上一页", (variablePage.url + (variablePage.pageNo - 1))); } } public String getNextpageNum() { if (variablePage.pageNo >= variablePage.totalPage) { return buildSpan("下一页", variablePage); } else { return buildA("下一页", variablePage.url + (variablePage.pageNo + 1)); } } public String buildSpan(String text, VariablePage variablePage) { return "<span style=https://www.jb51.net/article/\"color:gray;\">" + text + "</span>"; } public String buildA(String text, String url) { return "<a href=https://www.jb51.net/article/\"" + url + "https://www.jb51.net/article/\">" + text + "</a>"; } public String buildA(VariablePage variablePage, int num) { return ("<a href=https://www.jb51.net/article/\"" + variablePage.url + num + "https://www.jb51.net/article/\">" + num + "</a>"); } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzfzxz.html