First we need to create a file in the WEB-INF/tld/ directory
<?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>/taglibs/page</uri> <tag> <name>htmlPage</name> <tag-class></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 ; import ; import ; import ; import ; import ; import ; import ; /** * Pagination processing tags * * @description * @DATE 2012-3-11 09:08:46 pm */ 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) { = pageNo; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { = pageSize; } public int getTotalSum() { return totalSum; } public void setTotalSum(int totalSum) { = totalSum; } public int getShowPage() { return showPage; } public void setShowPage(int showPage) { = showPage; } public String getUrl() { return url; } public void setUrl(String url) { = url; } @Override public int doEndTag() throws JspException { if (pageSize == 0) { return TagSupport.SKIP_PAGE;// No pagination is displayed} else if (pageSize > totalSum) { return TagSupport.SKIP_BODY;// No pagination is displayed} JspWriter out = (); try { if ((";jsession?") == -1) { url += ";jsession?pageNo="; } else { url = ("\\?", ";jsession?"); if (("pageNo=") == -1) { url += "&pageNo="; } } } url = ().getContextPath() + url; VariablePage variablePage = new VariablePage(pageNo, totalSum, pageSize, url); (showPage); Page page = new HtmlPage(variablePage); (() + " " + () + " " + () + " " + () + " " + ()); } catch (IOException ex) { (); } return (); } @Override public void release() { url = null; pageNo = 0; totalSum = 0; pageSize = 10; (); } private static final long serialVersionUID = -2642907859641024483L; }
/** * HTML pagination class * * @description * @DATE 2012-3-11 10:33:14 pm */ public class HtmlPage extends Page { public HtmlPage(VariablePage variablePage) { super(variablePage); } public String pagination() { String printNo = ""; // If pages have been paging; and the number of pages is less than or equal to the number of pages to be displayedif ( > 1 && <= ) { return displayAll(); // If the number of pages: greater than the number of pages displayed} else if ( > 1 && > ) { if ( == 1) {// The current page is equal to the first pagereturn fromFirstPagePrint(); } else if ( == ) {// The current page is equal to the last pagereturn fromLastPagePrint(); } else {// If the current page: that is, it is not the home page or the last pageif ( % 2 == 0) {// Can be pagingint print$No = / 2; if ( >= print$No) { int index$No = - print$No; if ( + print$No >= ) { return fromLastPagePrint(); } else { if (index$No == 0) index$No = 1; for (int i = index$No; i < ( + index$No); i++) { if (i == ) {// If it is the current page: do not add the connection URLprintNo += (i + ); } else { printNo += (buildA(variablePage, i) + ); } } } } else { return fromFirstPagePrint(); } } else {// When the number of pages printed is not even:int print$No = / 2 + 1; if ( >= print$No && + print$No < ) { int index$No = - print$No + 1; for (int i = index$No; i < + index$No; i++) { if (i == ) {// If it is the current page: do not add the connection URLprintNo += (i + ); } else { printNo += (buildA(variablePage, i) + ); } } } else if ( <= print$No) {// Start from the first pagereturn fromFirstPagePrint(); } else { return fromLastPagePrint(); } } } return (printNo); } else { return "1"; } } public String getBackpageNum() { if ( <= 1) { return buildSpan("Previous Page", variablePage); } else { return buildA("Previous Page", ( + ( - 1))); } } public String getNextpageNum() { if ( >= ) { return buildSpan("Next Page", variablePage); } else { return buildA("Next Page", + ( + 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=\"" + + num + "\">" + num + "</a>"); } }
public abstract class Page { protected VariablePage variablePage; public Page(VariablePage variablePage) { = variablePage; calculateTotalPage(); } public int getStartIndex() { return (getValidpageNum() - 1) * ; } public String getBackpageNum() { if ( <= 1) { return buildSpan("Previous Page", variablePage); } else { return buildA("Previous Page", () + ( - 1)); } } public String getNextpageNum() { if ( >= ) { return buildSpan("Next Page", variablePage); } else { return buildA("Next Page", () + ( + 1)); } } /** * Calculate the total number of pages */ private void calculateTotalPage() { if ( % == 0) { = / ; } else { = / + 1; } if ( < ) { = ; } else if ( < 1) { = 1; } } protected String displayAll() { StringBuilder sBuilder = new StringBuilder(10); (); for (int i = 1; i <= ; i++) { if (i == ) { (i + ); } else { (buildA(variablePage, i) + ); } } return (); } /** * Abstract the paging method * * @return */ public abstract String pagination(); /** * Implement printing from the first page * * @return */ protected final String fromFirstPagePrint() { StringBuffer buffer = new StringBuffer(100); for (int i = 1; i <= ; i++) { if (i == ) {// If it is the current page: do not add the connection URL(i + ); } else { (buildA(variablePage, i) + ); } } return (); } /** * Implementation to print from the last page * * @return */ protected final String fromLastPagePrint() { StringBuffer buffer = new StringBuffer(100); int startPage = - ( - 1); for (int i = startPage; i <= ; i++) { if (i == ) {// If it is the current page: do not add the connection URL(i + ); } else { (buildA(variablePage, i) + ); } } return (); } public String getFirstNo() { if (isExistsPagination()) { return buildA("front page", + 1); } else { return buildSpan("front page", variablePage); } } /** * Determine whether paging exists * * @return */ private boolean isExistsPagination() { if ( > 1 && > 1) { return true; } return false; } public String getLastNo() { if (isExistsPagination()) { return buildA("Last Page", + ); } else { return buildSpan("Last Page", variablePage); } } protected int getValidpageNum() { if ( < ) { return = ; } else if ( < 1) { return = 1; } else { return ; } } public VariablePage getPageContant() { return ; } 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); ("Each page<input type='text' id='pageSize' style='width:20px' maxlength='2' name='pageSize'"); if(pageSize>0){ (" value='"+pageSize+"' "); } ("/>"); return (); } }
/** * Pagination basic variables * * @description * @DATE 2012-3-11 10:34:00 pm */ public class SimpleVariable { /** * Show page number */ protected int showPageNum = 10; /** * Connection URL */ protected String url = null; /** * Current page number */ protected int pageNo = 1; /** * Total page number */ protected int totalPage = 1; /** * Total number of */ protected int totalSum = 0; /** * Number of pieces displayed per page */ protected int pageSize = 10; public String getUrl() { return url; } public void setUrl(String url) { = url; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { = totalPage; } public int getTotalSum() { return totalSum; } public void setTotalSum(int totalSum) { = totalSum; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { = pageSize; } public int getShowPageNum() { return showPageNum; } public void setShowPageNum(int showPageNum) { = showPageNum; } public int getPageNo() { return pageNo; } public void setPageNo(int pageNo) { = pageNo; } }
/** * Pagination variables * * @description * @DATE 2012-3-11 10:34:08 pm */ 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; = pageNo; = totalSum; = url; = pageSize; } public VariablePage(int pageNum, int totalSum, String url) { pageNum = pageNum < 1 ? 1 : pageNum; = pageNum; = totalSum; = url; } public String getSplit() { return split; } public void setSplit(String split) { = split; } }
#The JSP that requires the pagination tag is used as follows:
JSP header introduction
<%@taglib uri="/taglibs/page" prefix="page" %>
Generate paging HTML code using the following tag
<page:htmlPage pageNo="${pageNo}" url="your_url" totalSum="${totalSum}" showPage="10" pageSize="12"/>