JSP自定义分页标签TAG全过程
程序员文章站
2022-06-11 14:20:22
首先我们需要在web-inf/tld/目录下创建page.tld文件
首先我们需要在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>http://www.kevin.com/taglibs/page</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=\"color:gray;\">" + text + "</span>"; } public string builda(string text, string url) { return "<a href=\"" + url + "\">" + text + "</a>"; } public string builda(variablepage variablepage, int num) { return ("<a href=\"" + variablepage.url + num + "\">" + num + "</a>"); } }
public abstract class page { protected variablepage variablepage; public page(variablepage variablepage) { this.variablepage = variablepage; calculatetotalpage(); } public int getstartindex() { return (getvalidpagenum() - 1) * variablepage.pagesize; } public string getbackpagenum() { if (variablepage.pageno <= 1) { return buildspan("上一页", variablepage); } else { return builda("上一页", variablepage.geturl() + (variablepage.pageno - 1)); } } public string getnextpagenum() { if (variablepage.pageno >= variablepage.totalpage) { return buildspan("下一页", variablepage); } else { return builda("下一页", variablepage.geturl() + (variablepage.pageno + 1)); } } /** * 计算总页数 */ private void calculatetotalpage() { if (variablepage.totalsum % variablepage.pagesize == 0) { variablepage.totalpage = variablepage.totalsum / variablepage.pagesize; } else { variablepage.totalpage = variablepage.totalsum / variablepage.pagesize + 1; } if (variablepage.totalpage < variablepage.pageno) { variablepage.pageno = variablepage.totalpage; } else if (variablepage.pageno < 1) { variablepage.pageno = 1; } } protected string displayall() { stringbuilder sbuilder = new stringbuilder(10); sbuilder.append(variablepage.split); for (int i = 1; i <= variablepage.totalpage; i++) { if (i == variablepage.pageno) { sbuilder.append(i + variablepage.split); } else { sbuilder.append(builda(variablepage, i) + variablepage.split); } } return sbuilder.tostring(); } /** * 抽象出分页方法 * * @return */ public abstract string pagination(); /** * 实现从第一页开始打印 * * @return */ protected final string fromfirstpageprint() { stringbuffer buffer = new stringbuffer(100); for (int i = 1; i <= variablepage.showpagenum; i++) { if (i == variablepage.pageno) {// 如果是当前页:不添加连接url buffer.append(i + variablepage.split); } else { buffer.append(builda(variablepage, i) + variablepage.split); } } return buffer.tostring(); } /** * 实现从最后一页开始打印 * * @return */ protected final string fromlastpageprint() { stringbuffer buffer = new stringbuffer(100); int startpage = variablepage.totalpage - (variablepage.showpagenum - 1); for (int i = startpage; i <= variablepage.totalpage; i++) { if (i == variablepage.pageno) {// 如果是当前页:不添加连接url buffer.append(i + variablepage.split); } else { buffer.append(builda(variablepage, i) + variablepage.split); } } return buffer.tostring(); } public string getfirstno() { if (isexistspagination()) { return builda("首页", variablepage.url + 1); } else { return buildspan("首页", variablepage); } } /** * 判断是否存在分页 * * @return */ private boolean isexistspagination() { if (variablepage.totalsum > 1 && variablepage.totalpage > 1) { return true; } return false; } public string getlastno() { if (isexistspagination()) { return builda("尾页", variablepage.url + variablepage.totalpage); } else { return buildspan("尾页", variablepage); } } protected int getvalidpagenum() { if (variablepage.totalpage < variablepage.pageno) { return variablepage.pageno = variablepage.totalpage; } else if (variablepage.pageno < 1) { return variablepage.pageno = 1; } else { return variablepage.pageno; } } public variablepage getpagecontant() { return this.variablepage; } public abstract string buildspan(string text, variablepage variablepage); public abstract string builda(string text, string url); public abstract string builda(variablepage variablepage, int num); public string printmodifypagesize(int pagesize){ stringbuilder builder=new stringbuilder(100); builder.append("每页<input type='text' id='pagesize' style='width:20px' maxlength='2' name='pagesize'"); if(pagesize>0){ builder.append(" value='"+pagesize+"' "); } builder.append("/>条"); return builder.tostring(); } }
/** * 分页基础变量 * * @description * @date 2012-3-11下午10:34:00 */ public class simplevariable { /** * 显示页码 */ protected int showpagenum = 10; /** * 连接url */ protected string url = null; /** * 当前页码 */ protected int pageno = 1; /** * 总页码 */ protected int totalpage = 1; /** * 总条数 */ protected int totalsum = 0; /** * 每页显示条数 */ protected int pagesize = 10; public string geturl() { return url; } public void seturl(string url) { this.url = url; } public int gettotalpage() { return totalpage; } public void settotalpage(int totalpage) { this.totalpage = totalpage; } public int gettotalsum() { return totalsum; } public void settotalsum(int totalsum) { this.totalsum = totalsum; } public int getpagesize() { return pagesize; } public void setpagesize(int pagesize) { this.pagesize = pagesize; } public int getshowpagenum() { return showpagenum; } public void setshowpagenum(int showpagenum) { this.showpagenum = showpagenum; } public int getpageno() { return pageno; } public void setpageno(int pageno) { this.pageno = pageno; } }
/** * 分页变量 * * @description * @date 2012-3-11下午10:34:08 */ public class variablepage extends simplevariable { protected string split = " "; protected string style = "class='page'"; public variablepage(int pageno, int totalsum, int pagesize, string url) { pageno = pageno < 1 ? 1 : pageno; super.pageno = pageno; this.totalsum = totalsum; this.url = url; this.pagesize = pagesize; } public variablepage(int pagenum, int totalsum, string url) { pagenum = pagenum < 1 ? 1 : pagenum; super.pageno = pagenum; this.totalsum = totalsum; this.url = url; } public string getsplit() { return split; } public void setsplit(string split) { this.split = split; } }
#需要使用该分页标签的jsp 使用方法如下:
jsp 头部引入
<%@taglib uri="http://www.kevin.com/taglibs/page" prefix="page" %>
使用以下标签生成分页html代码
<page:htmlpage pageno="${pageno}" url="your_url" totalsum="${totalsum}" showpage="10" pagesize="12"/>
下一篇: JSP 自动刷新的实例详解