package com.yzr.utils; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.SimpleTagSupport; /** * 分页标签 * * @author 伊昭荣 * @Time 2013年6月2日 22:45:58 */ public class PageTag extends SimpleTagSupport { /** * 总记录数,默认为0 */ private Integer count = 0; /** * 总页数 */ private Integer pageCount; /** * 当前页,默认设置为1 */ private Integer page = 1; /** * 每页显示的条数,默认显示10条 */ private Integer rows = 10; /** * 页面链接 */ private String url; /** * 页面参数名称,默认为page */ private String params = "page"; /** * 显示给前台的东西 */ @Override public void doTag() throws JspException, IOException { JspWriter out = getJspContext().getOut(); StringBuffer pageList = new StringBuffer(); pageCount = (pageCount == null || pageCount < 1) ? 0 : pageCount; /** * 计算当前总页数 */ pageCount = count % rows > 0 ? count / rows + 1 : count / rows; page = (page == null || page < 1 || page > pageCount) ? 1 : page; pageList.append("<div class=\"pagination\">"); /** * 当总页数大于1 */ if (pageCount > 1) { pageList.append("<div class=\"pagination-pages\">"); pageList.append("<div class=\"pagination-page\">"); /** * 显示上一页 */ if (page > 1) { pageList.append("<a class=\"page-prev\" "); pageList.append("href=\"" + url + "?"); pageList.append("" + params + "=" + (page - 1) + "\""); pageList.append("title=\"上一页\">"); pageList.append("<span>上一页</span></a>"); } /** * 总页数小于10,全部显示 */ if (pageCount <= 10) { for (int i = 1; i <= pageCount; i++) { /** * 不是当前页 */ if (i != page) { pageList.append("<a class=\"page\" title=\"第" + i + "页\""); pageList.append("href=\"" + url + "?"); pageList.append("" + params + "=" + i + "\">" + i + "</a>"); } else { /** * 是当前页 */ pageList.append("<span class=\"page-cur\">" + i + "</span>"); } } } else { /** * 总页数大于10,分情况显示 */ int i = page - 3 <= 2 ? 1 : page - 3; int end = page + 3 > pageCount ? pageCount : page + 3; /** * 当前页加上3页不到7页时显示7页 */ end = end > 7 ? end : 7; i = end == pageCount ? pageCount - 7 : i; /** * 显示第1、2页 */ if (i > 2) { for (int j = 1; j < 3; j++) { pageList.append("<a class=\"page\" title=\"第" + j + "页\""); pageList.append("href=\"" + url + "?"); pageList.append("" + params + "=" + j + "\">" + j + "</a>"); } if (page - 3 > 3) pageList.append("<span class=\"page-split\">...</span>"); } for (; i <= end; i++) { /** * 不是当前页 */ if (i != page) { pageList.append("<a class=\"page\" title=\"第" + i + "页\""); pageList.append("href=\"" + url + "?"); pageList.append("" + params + "=" + i + "\">" + i + "</a>"); } else { /** * 是当前页 */ pageList.append("<span class=\"page-cur\">" + i + "</span>"); } } if (end < pageCount) { if (end + 1 < pageCount) pageList.append("<span class=\"page-split\">...</span>"); pageList.append("<a class=\"page\" title=\"第" + pageCount + "页\""); pageList.append("href=\"" + url + "?"); pageList.append("" + params + "=" + pageCount + "\">" + pageCount + "</a>"); } } /** * 当总页数大于1,当前页小于总页数时,下一页可点 */ if (page < pageCount) { pageList.append("<a class=\"page-next\""); pageList.append("href=\"" + url + "?"); pageList.append("" + params + "=" + (page + 1) + "\""); pageList.append("title=\"下一页\">"); pageList.append("<span>下一页</span>"); pageList.append("</a>"); } pageList.append("</div>"); /** * 显示总页数 */ pageList.append("<div class=\"pagination-count\">"); pageList.append("<span>共</span>"); pageList.append("<span class=\"b\">" + pageCount != null ? pageCount : 0 + "</span>"); pageList.append("<span>页</span>"); pageList.append("</div>"); /** * 进行页面跳转 */ int nextPage = page + 1 > pageCount ? pageCount : page + 1; pageList.append("<div class=\"pagination-form\"><span>向第</span>"); pageList.append("<input type=\"text\" name=\"page\" value=\"" + nextPage + "\" class=\"page-num\">"); pageList.append("<span>页</span><a href=\"javascript:getPage('" + url + "')\" class=\"btn\">跳转</a></div>"); } else { pageList.append("<div class=\"pagination-noData\">没有相关数据</div>"); } pageList.append("</div>"); out.write(pageList.toString()); } public Integer getCount() { return count; } public void setCount(Integer count) { this.count = count; } public Integer getPageCount() { return pageCount; } public void setPageCount(Integer pageCount) { this.pageCount = pageCount; } public Integer getPage() { return page; } public void setPage(Integer page) { this.page = page; } public Integer getRows() { return rows; } public void setRows(Integer rows) { this.rows = rows; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getParams() { return params; } public void setParams(String params) { this.params = params; } }
page.tld文件
<?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>1.0</tlib-version> <short-name>p</short-name> <uri>/WEB-INF/tlds/page</uri> <tag> <name>page</name> <tag-class>com.yzr.utils.PageTag</tag-class> <body-content>empty</body-content> <attribute> <name>url</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.String</type> </attribute> <attribute> <name>count</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <type>int</type> </attribute> <attribute> <name>pageCount</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <type>int</type> </attribute> <attribute> <name>page</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <type>int</type> </attribute> <attribute> <name>rows</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <type>int</type> </attribute> <attribute> <name>params</name> <rtexprvalue>false</rtexprvalue> <type>java.lang.String</type> </attribute> </tag> </taglib>
package com.yzr.servlet; import java.io.*; import javax.servlet.*; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; @WebServlet(name = "PagesServlet", urlPatterns = { "/PagesServlet.go" }) public class PagesServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String curr = request.getParameter("page"); if (curr == null) { curr = "1"; } request.setAttribute("page", curr); RequestDispatcher rd = request.getRequestDispatcher("page.jsp"); rd.forward(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } public String getServletInfo() { return "Short description"; } }
page.jsp文件
<%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <%@taglib prefix="p" uri="/WEB-INF/tlds/page.tld"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> <link rel="stylesheet" type="text/css" href="css/page.css"> <script type="text/javascript" src="js/jquery-1.8.2.min.js"></script> <script type="text/javascript" src="js/page.js"></script> </head> <body> <h1>JSP Page</h1> <p:page url="PagesServlet.go" page="${page}" count="1001" /> <p> </p> </body> </html>