package mytags; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.TagSupport; public class PagerTag extends TagSupport { /** * */ private static final long serialVersionUID = -3066943125964706748L; private String url; //表单提交地址 private int recordCount; //记录总数 private int pageNo; //当前页码 private int pageSize; //页面容量 //标签体的主体方法 @Override public int doStartTag() throws JspException { // TODO Auto-generated method stub //总页数 int pageCount=(recordCount+pageSize-1)/pageSize; if(pageNo>pageCount){ pageNo=pageCount; } if(pageNo<0){ pageNo=1; } //提交到标签体的表单及脚本内容 StringBuffer sb=new StringBuffer(); sb.append("<div class=\"pagination\">"); //当action=“”时,表示不刷新页面,只更新表单内容 sb.append("<form action=\"\" method=\"post\" name=\"qPagerForm\">\r\n"); //传递当前页码的隐式参数 sb.append("<input type=\"hidden\" name=\"pageNo\" value=") .append(pageNo).append(">"); //是否显示“上一页”链接 if(pageNo>1){ sb.append("<a href=\"javascript:void(null)\" onclick=\"turnOverPage(" + (pageNo-1) + ")\">"); sb.append("pre"); sb.append("</a>"); } //是否显示当前页前面的"...",以及其它前面的显示逻辑 int start=1; if(pageNo>4){ start=pageNo-1; sb.append("<a href=\"javascript:void(null)\" onclick=\"turnOverPage(1)\">"); sb.append("1"); sb.append("</a>"); sb.append("<a href=\"javascript:void(null)\" onclick=\"turnOverPage(2)\">"); sb.append("2"); sb.append("</a>"); sb.append("…"); } int end=pageNo+1; if(end>pageCount){ end=pageCount; } for(int i=start;i<=end;i++){ if(i==pageNo){ sb.append("<span class=\"current\">") .append(pageNo) .append("</span>"); }else{ sb.append("<a href=\"javascript:void(null)\" onclick=\"turnOverPage(") .append(i).append(")\">").append(i).append("</a>"); } } //当前页面后面的显示逻辑 if(end<pageCount-2){ sb.append("…"); } if(end<pageCount-1){ sb.append("<a href=\"javascript:void(null)\" onclick=\"turnOverPage(") .append(pageCount-1).append(")\">").append(pageCount-1).append("</a>"); } if(end<pageCount){ sb.append("<a href=\"javascript:void(null)\" onclick=\"turnOverPage(") .append(pageCount).append(")\">").append(pageCount).append("</a>"); //显示”下一页“链接2 sb.append("<a href=\"javascript:void(null)\" onclick=\"turnOverPage(") .append(pageNo+1).append(")\">").append("next").append("</a>"); } sb.append("</form></div>"); //翻页脚本函数,主要作用: //(1)向TestPagerServlet提交跳转页码,更新条目内容的显示 //(2)更新翻页标签的显示 //脚本函数是隐式的,不会显示 sb.append("<script language=\"javascript\">\r\n"); sb.append("function turnOverPage(no){\r\n"); //脚本与表单属于同一文档,下面是获取表单的方法 sb.append("var qForm=document.qPagerForm;\r\n"); sb.append("if(no>").append(pageCount) .append("){no=") .append(pageCount) .append(";}\r\n"); sb.append("if(no<1){no=1;}\r\n"); sb.append("qForm.pageNo.value=no;\r\n") .append("qForm.action=\"").append(url).append("\";"); sb.append("qForm.submit();").append("\r\n}"); sb.append("</script>"); try { pageContext.getOut().println(sb.toString()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return SKIP_BODY; } public void setRecordCount(int num){ recordCount=num; } public void setPageNo(int num){ pageNo=num; } public void setPageSize(int num){ pageSize=num; } public void setUrl(String str){ url=str; } }
import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class TestPagerTagServlet */ @WebServlet("/TestPagerTagServlet") public class TestPagerTagServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static final int PAGE_SIZE=10; private List<String> datas; /** * @see HttpServlet#HttpServlet() */ public TestPagerTagServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub int pageSize= PAGE_SIZE; request.setAttribute("pageSize", pageSize); int recordCount=datas.size(); //获取当前页码 String no=request.getParameter("pageNo"); int pageNo=1; if(!"".equals(no) && no!=null){ pageNo=Integer.parseInt(no); } request.setAttribute("recordCount",recordCount); request.setAttribute("pageNo", pageNo); int s=(pageNo-1)*pageSize; int e=s+pageSize; if(e>recordCount){ e=recordCount; } List<String> subdatas=datas.subList(s, e); request.setAttribute("subdatas", subdatas); request.getRequestDispatcher("pager.jsp").forward(request, response); } @Override public void init() throws ServletException { // TODO Auto-generated method stub //super.init(); //生成测试用的数据 datas = new ArrayList<String>(); for(int i=1;i<105;i++){ String str; str="String"+i; datas.add(str); } } }pager.tdl
<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?> <taglib> <tlib-version>0.0</tlib-version> <jsp-version>0.0</jsp-version> <short-name>q</short-name> <!--统一资源定位符,唯一即可--> <uri>http://www.csdn.net/cjc/tags/pagertag</uri> <!-- 自定义标签的描述信息 --> <tag> <!-- 标签名 --> <name>pager</name> <!-- 对应的标签处理类全限定名 --> <tag-class>mytags.PagerTag</tag-class> <!-- 标签主体的类型 --> <body-content>empty</body-content> <!-- 当前页号属性的描述信息 --> <attribute> <!-- 属性名 --> <name>pageNo</name> <!-- 该属性是否为必要的 --> <required>true</required> <!-- 属性值是否可以在JSP运行时期动态产生 --> <rtexprvalue>true</rtexprvalue> <!-- 属性的数据类型 --> <type>int</type> </attribute> <!-- 总记录数属性的描述信息 --> <attribute> <name>recordCount</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>int</type> </attribute> <!-- 总页数属性的描述信息 --> <attribute> <name>pageSize</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>int</type> </attribute> <!-- 分页标签要跳转的URI属性的描述信息 --> <attribute> <name>url</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.String</type> </attribute> </tag> </taglib>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" import ="java.util.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib uri="http://www.csdn.net/cjc/tags/pagertag" prefix="q" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>My Define Pager</title> <style type="text/css"> .pagination{padding:5px; float:right;} .pagination a, .pagination a:link, .pagination a:visted { padding: 2px 5px 2px 5px; margin: 2px; border: 1px solid #aaaadd; text-decoration: none; color: #006699; } .pagination a:hover, .pagination a:active { border: 1px solid #ff0000; color: #000; text-decoration: none; } .pagination span.current { padding: 2px 5px 2px 5px; margin: 2px; border: 1px solid #ff0000; font-weight: bold; background-color: #ff0000; color: #FFF; } </style> </head> <body> <table cellspacing="1" cellpadding="5" border="1" width="500" align="center"> <% out.println("<hr/>"); List<String> datas = (List<String>)request.getAttribute("subdatas"); for(int i=0;i<datas.size();i++){ out.println("<tr><td>"); out.println(datas.get(i)); out.println("</td></tr>"); } %> </table> <!--EL表达式 --> <q:pager pageNo="${pageNo}" pageSize="${pageSize}" recordCount="${recordCount}" url="TestPagerTagServlet" /> </body> </html>index.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <a href="TestPagerTagServlet">Test it now</a> </body> </html>
原文:http://blog.csdn.net/cjc211322/article/details/41629499