分页标签


1.WEB-INF/tlds/pager.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>0.9</tlib-version>
   <!--标签头-->
	<short-name>w</short-name>
    <!--将来在页面用taglib引用时的uri属性,这部分的名字可以随便写,只要是符合HTTP网址形式的	-->
	<uri>http://javacrazyer.javaeye.com/tags/pager</uri>
	
	<!-- 自定义标签的描述信息 -->
	<tag>
		<!-- 标签名 -->
		<name>pager</name>
		<!-- 对应的标签处理类全限定名 -->
		<tag-class>com.kington.ibank.util.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>


2.WEB-INF/web.xml

<filter>
    	<filter-name>securityFilter</filter-name>
    	<filter-class>com.kington.ibank.webapp.security.SecurityFilter</filter-class>
    </filter>
    
    <jsp-config>   
        <taglib>   
            <!-- 配置标签的引用地址 JSP页面中引用时使用-->   
            <taglib-uri>/eltag</taglib-uri>   
            <!-- 配置标签的TLD文件地址 -->   
            <taglib-location>/WEB-INF/tlds/ElTag.tld</taglib-location>   
        </taglib> 
         <taglib>   
            <!-- 配置标签的引用地址 JSP页面中引用时使用-->   
            <taglib-uri>/taglib</taglib-uri>   
            <!-- 配置标签的TLD文件地址 -->   
            <taglib-location>/WEB-INF/tlds/TagLib.tld</taglib-location>   
        </taglib>  
         <taglib>   
            <!-- 配置标签的引用地址 JSP页面中引用时使用-->   
            <taglib-uri>/mypage</taglib-uri>   
            <!-- 配置标签的TLD文件地址 -->   
            <taglib-location>/WEB-INF/tlds/pager.tld</taglib-location>   
        </taglib>  
           <taglib>
	      <taglib-uri>/ss3/urlIntercept</taglib-uri>
	      <taglib-location>/WEB-INF/tlds/security.tld</taglib-location>
    	</taglib>
    </jsp-config>   
    
    <listener>
        <listener-class>com.kington.ibank.util.SchedulerInit</listener-class>
  	</listener>


3.PageTag.java

package com.kington.ibank.util;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.struts2.ServletActionContext;


/**
 * 分页标签处理类
 */
public class PagerTag extends TagSupport {
	private static final long serialVersionUID = 5729832874890369508L;
	private String url;         //请求URI
	private int pageSize = 20;  //每页要显示的记录数
	private int pageNo = 1;     //当前页号
	private int recordCount;    //总记录数

	@SuppressWarnings("unchecked")
	public int doStartTag() throws JspException {
		HttpServletRequest request= ServletActionContext.getRequest();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
		int pageCount = (recordCount + pageSize - 1) / pageSize;  //计算总页数
		
		//拼写要输出到页面的HTML文本
		StringBuilder sb = new StringBuilder();
		if(recordCount == 0){
			sb.append("<strong>没有可显示的项目</strong>\r\n");
		}else{
			//页号越界处理
			if(pageNo > pageCount){		pageNo = pageCount;	}
			if(pageNo < 1){		pageNo = 1;	}
			
			sb.append("<form method=\"post\" action=\"").append(basePath+((HttpServletRequest) request).getContextPath()+this.url)
				.append("\" name=\"qPagerForm\">\r\n");
			//获取请求中的所有参数
			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("<input type=\"hidden\" name=\"").append("pageNo")
				.append("\" value=\"").append(pageNo).append("\"/>\r\n");
			
			// 输出统计数据
		/**	sb.append("&nbsp;共<strong>").append(recordCount)
				.append("</strong>项")
				.append(",<strong>")
				.append(pageCount)
				.append("</strong>页:&nbsp;\r\n");**/
			sb.append("<ul>");
			
			//上一页处理
			if (pageNo == 1) {
				sb.append("<li><span class=\"disabled\">上一页")
					.append("</span></li>\r\n");
			} else {
				sb.append("<li><a href=\"javascript:turnOverPage(")
				  .append((pageNo - 1))
				  .append(")\">上一页</a></li>\r\n");
			}
			
			//如果前面页数过多,显示"..."
			int start = 1; 
			if(this.pageNo > 4){
				start = this.pageNo - 1;
				sb.append("<li><a href=\"javascript:turnOverPage(1)\">1</a></li>\r\n");
				sb.append("<li><a href=\"javascript:turnOverPage(2)\">2</a></li>\r\n");
				sb.append("<li><a>&hellip;</a></li>\r\n");
			}
			//显示当前页附近的页
			int end = this.pageNo + 1;
			if(end > pageCount){
				end = pageCount;
			}
			for(int i = start; i <= end; i++){
				if(pageNo == i){   //当前页号不需要超链接
					sb.append("<li><span class=\"current\">")
						.append(i)
						.append("</span></li>\r\n");
				}else{
					sb.append("<li><a href=\"javascript:turnOverPage(")
						.append(i)
						.append(")\">")
						.append(i)
						.append("</a></li>\r\n");
				}
			}
			//如果后面页数过多,显示"..."
			if(end < pageCount - 2){
				sb.append("<li><a>&hellip;</a></li>\r\n");
			}
			if(end < pageCount - 1){
				sb.append("<li><a href=\"javascript:turnOverPage(")
				.append(pageCount - 1)
				.append(")\">")
				.append(pageCount - 1)
				.append("</a></li>\r\n");
			}
			if(end < pageCount){
				sb.append("<li><a href=\"javascript:turnOverPage(")
				.append(pageCount)
				.append(")\">")
				.append(pageCount)
				.append("</a></li>\r\n"); 
			}
			
			//下一页处理
			if (pageNo == pageCount) {
				sb.append("<li><span class=\"disabled\">下一页")
					.append("</span></li>\r\n");
			} else {
				sb.append("<li><a href=\"javascript:turnOverPage(")
					.append((pageNo + 1))
					.append(")\">下一页</a></li>\r\n");
			}
	
			// 生成提交表单的JS
			sb.append("<script language=\"javascript\">\r\n");
			sb.append("  function turnOverPage(no){\r\n");
			sb.append("    if(no>").append(pageCount).append("){");
			sb.append("      no=").append(pageCount).append(";}\r\n");
			sb.append("    if(no<1){no=1;}\r\n");
			sb.append("    document.qPagerForm.pageNo.value=no;\r\n");
			sb.append("    document.qPagerForm.submit();\r\n");
			sb.append("  }\r\n");
			sb.append("</script>\r\n");
			sb.append("</ul>");
			sb.append("</form>\r\n");
		}
		//把生成的HTML输出到响应中
		try {
			pageContext.getOut().println(sb.toString());
		} catch (IOException e) {
			throw new JspException(e);
		}
		return SKIP_BODY;  //本标签主体为空,所以直接跳过主体
	}

	public void setUrl(String url) {
		this.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;
	}
}


4.使用这个标签

<c:if test="${not empty pageModel.list}">
	<div class="pagination pagination-small pagination-centered">
		<c:if test="${reading == 0 }">
	<w:pager pageSize="${pageModel.recordPage}" pageNo="${pageModel.curPage}" url="/banks/member/member/messages.jhtml" recordCount="${pageModel.fullListSize}"/> 
</c:if>
<c:if test="${reading == 1 }">
	<w:pager pageSize="${pageModel.recordPage}" pageNo="${pageModel.curPage}" url="/banks/member/member/messageUnread.jhtml" recordCount="${pageModel.fullListSize}"/> 
</c:if>
	</div>
</c:if>


5.输出结果:

<div class="pagination pagination-small pagination-centered">
	<form method="post" action="http://localhost:8080/ibank/banks/member/member/messageUnread.jhtml" name="qPagerForm">
		<input type="hidden" name="reading" value="1"/>
		<input type="hidden" name="messagesTotalCount" value="56"/>
		<input type="hidden" name="pageNo" value="1"/>
		<ul><li><span class="disabled">上一页</span></li>
		<li><span class="current">1</span></li>
		<li><a href="javascript:turnOverPage(2)">2</a></li>
		<li><a>&hellip;</a></li>
		<li><a href="javascript:turnOverPage(9)">9</a></li>
		<li><a href="javascript:turnOverPage(10)">10</a></li>
		<li><a href="javascript:turnOverPage(2)">下一页</a></li>
			<script language="javascript">
			  function turnOverPage(no){
			    if(no>10){      no=10;}
			    if(no<1){no=1;}
			    document.qPagerForm.pageNo.value=no;
			    document.qPagerForm.submit();
			  }
			</script>
		</ul>
	</form>
</div>

你可能感兴趣的:(PAGER)