一、标签处理类:
- /**
- * ClassName: PagerTag.java
- * created on Oct 21, 2008
- * Copyrights 2008 qjyong All rights reserved.
- * EMail: qjyong@gmail.com
- */
- package com.qiujy.tags;
- import java.io.IOException;
- import java.util.Enumeration;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.jsp.JspException;
- import javax.servlet.jsp.tagext.TagSupport;
- /**
- * 自定义分页标签
- * 使用方式 <my:pager pageSize="10"
- * pageNo="1"
- * recordCount="100"
- * url="index.jsp" />
- *
- * @author qiujy
- *
- */
- public class PagerTag extends TagSupport {
- private int pageSize = 10; // 每页要显示的记录数
- private int pageNo = 1; // 页号
- private int recordCount; // 总记录数
- private String url; // 目的地URL
- public void setPageSize(int pageSize) {
- this.pageSize = pageSize;
- }
- public void setPageNo(int pageNo) {
- this.pageNo = pageNo;
- }
- public void setRecordCount(int recordCount) {
- this.recordCount = recordCount;
- }
- public void setUrl(String url) {
- this.url = url;
- }
- // 主要的逻辑
- @Override
- public int doStartTag() throws JspException {
- if(recordCount == 0){
- return super.doStartTag();
- }
- // 总页数
- int pageCount = (recordCount + pageSize - 1) / pageSize;
- // 页号越界处理
- if (pageNo > pageCount) {
- pageNo = pageCount;
- }
- if (pageNo < 1) {
- pageNo = 1;
- }
- StringBuilder sb = new StringBuilder();
- sb.append("<form name='pageController' id='pageController' action='' method='post'>/r/n")
- .append("<input type='hidden' id='pageNo' name='pageNo' value='" + pageNo + "' />/r/n");
- //------------------------------------ 获取所有請求中的参数
- HttpServletRequest request = (HttpServletRequest) pageContext
- .getRequest();
- Enumeration<String> enumeration = request.getParameterNames();
- String name = null;
- String value = null;
- //把请求中的所有参数当作隐藏表单域在页面中写出)
- while (enumeration.hasMoreElements()) {
- name = enumeration.nextElement();
- value = request.getParameter(name);
- // 去除页号
- if (name.equals("pageNo")) {
- if (null != value && !"".equals(value)) {
- pageNo = Integer.parseInt(value);
- }
- continue;
- }
- sb.append("<input type='hidden' name='")
- .append(name)
- .append("' value='")
- .append(value)
- .append("'/>/r/n");
- }
- //----------------------------------------------------
- sb.append(" 总共有" + pageCount + "页,")
- .append("当前是第" + pageNo + "页 /r/n");
- if (pageNo == 1) {
- sb.append("首页");
- sb.append(" ");
- sb.append("上一页/r/n");
- } else {
- sb.append("<a href='#' οnclick='turnOverPage(1)'>首页</a>/r/n");
- sb.append(" ");
- sb.append("<a href='#' οnclick='turnOverPage(")
- .append((pageNo - 1)).append(")'>上一页</a>/r/n");
- }
- sb.append(" ");
- if (pageNo == pageCount) {
- sb.append("下一页");
- sb.append(" ");
- sb.append("尾页/r/n");
- } else {
- sb.append("<a href='#' οnclick='turnOverPage(")
- .append((pageNo + 1)).append(")'>下一页</a>/r/n");
- sb.append(" ");
- sb.append("<a href='#' οnclick='turnOverPage(").append(pageCount)
- .append(")'>尾页</a>/r/n");
- }
- sb.append(" 跳到<select onChange='turnOverPage(this.value)'>/r/n");
- for (int i = 1; i <= pageCount; i++) {
- if (i == pageNo) {
- sb.append(" <option value='")
- .append(i)
- .append("' selected='selected'>第")
- .append(i)
- .append("页</option>/r/n");
- } else {
- sb.append(" <option value='")
- .append(i)
- .append("'>第")
- .append(i)
- .append("页</option>/r/n");
- }
- }
- sb.append("</select>/r/n");
- sb.append(" /r/n");
- sb.append("</form>/r/n");
- // 生成提交表单的JS
- sb.append("<script language='javascript'>/r/n");
- sb.append(" //翻页函数/t/n");
- sb.append(" function turnOverPage(no){/r/n");
- sb.append(" var form = document.pageController;/r/n");
- sb.append(" //页号越界处理/r/n");
- sb.append(" if(no").append(">").append(pageCount).append(") {/r/n");
- sb.append(" no=").append(pageCount).append(";/r/n");
- sb.append(" }/r/n");
- sb.append(" if(no").append("< 1){/r/n");
- sb.append(" no=1;/r/n");
- sb.append(" }/r/n");
- sb.append(" form.").append("pageNo").append(".value=no;/r/n");
- sb.append(" form.action='").append(url).append("';/r/n");
- sb.append(" form.submit();/r/n");
- sb.append(" }/r/n");
- sb.append("</script>/r/n");
- try {
- pageContext.getOut().println(sb.toString());
- } catch (IOException e) {
- e.printStackTrace();
- }
- return super.doStartTag();
- }
- }
二、标签库描述文件:
- <?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>2.0</tlib-version>
- <short-name>my</short-name>
- <uri>http://blog.csdn.net/qjyong</uri>
- <tag>
- <description>通用分页标签</description>
- <name>pager</name>
- <tag-class>com.qiujy.tags.PagerTag</tag-class>
- <body-content>empty</body-content>
- <attribute>
- <name>pageNo</name>
- <required>true</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>recordCount</name>
- <required>true</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>pageSize</name>
- <required>true</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>url</name>
- <required>true</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- </tag>
- </taglib>
三、把标签处理类和tld打成pager.jar包。使用时只需添加到项目的类路径中即可。
四、测试:先写一个Servlet来创建测试数据
- package com.qiujy.web.servlet;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class MyServlet extends HttpServlet {
- public static List<String> list = new ArrayList<String>();
- public MyServlet(){
- for(int i = 0; i < 100; i++){
- list.add("aaa" + i);
- }
- }
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- this.doPost(request, response);
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String pageNoStr = request.getParameter("pageNo");
- int pageNo = 1;
- if(pageNoStr != null && !pageNoStr.equals("")){
- pageNo = Integer.parseInt(pageNoStr);
- }
- int recordCount = list.size();
- int pageSize = 10;
- int startNo = (pageNo - 1) * pageSize;
- List<String> result = list.subList(startNo, startNo + 10);
- request.setAttribute("recordCount", recordCount);
- request.setAttribute("pageNo", pageNo);
- request.setAttribute("pageSize", pageSize);
- request.setAttribute("data", result);
- request.getRequestDispatcher("index.jsp").forward(request, response);
- }
- }
web.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4" 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
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <servlet>
- <servlet-name>pageTestServlet</servlet-name>
- <servlet-class>com.qiujy.web.servlet.MyServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>pageTestServlet</servlet-name>
- <url-pattern>/testpager</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
页面:index.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@ taglib uri="http://blog.csdn.net/qjyong" prefix="q" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>PagerTag使用示例</title>
- </head>
- <body>
- <%
- List<String> list = (List<String>)request.getAttribute("data");
- for(String str : list){
- out.println(str + "<br/>");
- }
- %>
- <q:pager pageNo="${pageNo}"
- recordCount="${recordCount}"
- pageSize="${pageSize}"
- url="testpager" />
- </body>
- </html>
访问testpager后的效果图: