jsp 分页

 最近学习做分页,在网上发现一位高手提供了源码

blog:http://hi.baidu.com/wen870105/blog/item/180739276d97cc01918f9d3a.html

源码地址:http://wnick.iteye.com/blog/242945

分页的效果:

 

收获很大,非常感谢!~

 

以下内容为自己学习编码过程,记录下供以后使用

1. 自定义了一个paging标签,负责控制标签的内容并显示

PangingTag.java:

import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class PagingTag extends BodyTagSupport {
	private static final long serialVersionUID = 1L;
	private int numFound;
	private int currentPage;
	private int pageCount;
	private int pageSize = 10;
	private String url = "";
	private String params = "";
	
	@Override
	public int doEndTag() throws JspException {
		return super.doEndTag();
	}
	@Override
	public int doStartTag() throws JspException {

		String paramsUrl = url;
		if (!params.equals("")) {
			paramsUrl = url + "?" + params;
		}
		StringBuffer buf = new StringBuffer();

		// 当前为第一页
		if (currentPage == 1) {
			buf.append(" 首页 ");
			buf.append(" 上一页 ");
		} else {
			buf.append("<a href=\"" + paramsUrl + "&page=1\""+ ">首页</a> ");
			buf.append(" <a href=\"" + paramsUrl + "&page=" + (currentPage-1) + "\">上一页</a>");
		}
		// 总页数不超过 10
		if (pageCount <= 10) {
			for (int i = 1; i <= pageCount; i++) {
				if (i == currentPage) {
					buf.append(" " + i + " ");
				} else {
					buf.append(" <a href=\"" + paramsUrl + "&page=" + i +"\">[" + i + "]</a> ");
				}
			}
		} else {
			// 分页
			int front = currentPage - 5;
			int back = currentPage + 4;
			int start = Math.max(front, 1);
			for (int i = start; i < currentPage; i++) {
				buf.append(" <a href=\"" + paramsUrl + "&page=" + i +"\">[" + i + "]</a> ");
			}
			buf.append(" " + currentPage + " ");
			int end;
			if (back <= 10) {
				end = 10;
			} else {
				end = Math.min(back, pageCount);
			}
			for (int i = currentPage+1; i <= end; i++) {
				buf.append(" <a href=\"" + paramsUrl + "&page=" + i +"\">[" + i + "]</a> ");
			}
		}
		// 当前为最后一页
		if (currentPage == pageCount) {
			buf.append(" 下一页 ");
			buf.append(" 最后一页 ");
		} else {
			buf.append(" <a href=\"" + paramsUrl + "&page=" + (currentPage+1) + "\">下一页</a> ");
			buf.append(" <a href=\"" + paramsUrl + "&page=" + pageCount + "\">最后一页</a> ");
		}
		buf.append("  <font color=\"red\">" + currentPage + "</font>/<font color=\"\">" 
		                + pageCount	+ "</font>");
		try {
			pageContext.getOut().println(buf.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}
	
	// getter and setter
	public int getNumFound() {
		return numFound;
	}
	public void setNumFound(int numFound) {
		this.numFound = numFound;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	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;
	}
	
}

 

2. 在WEB-INF/lib目录下,新建一个paging.tld文件,描述自定义的paging标签。eclipse下新建tld文件

paging.tld目录

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd" >
<taglib xmlns="http://java.sun.com/JSP/TagLibraryDescriptor">
	<tlib-version>1.1</tlib-version>
	<jsp-version>1.2</jsp-version>
	<short-name>page</short-name>
	<tag>
		<name>paging</name>
		<tag-class>DDiao.Servlet.PagingTag</tag-class>
		<body-content>jsp</body-content>
		<attribute>
			<name>currentPage</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<type>int</type>
			<description>当前页面的页数</description>
		</attribute>
		<attribute>
			<name>pageCount</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<type>int</type>
			<description>总页数</description>
		</attribute>
		<attribute>
			<name>url</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<type>String</type>
			<description>url地址</description>
		</attribute>
		<attribute>
			<name>params</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<type>String</type>
			<description>url地址后面的参数,除page外的其它参数,翻页时保持不变</description>
		</attribute>
		<attribute>
			<name>numFound</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
			<type>int</type>
			<description>搜索结果的总数目</description>
		</attribute>
		<attribute>
			<name>pageSize</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
			<type>int</type>
			<description>页面显示记录的数目</description>
		</attribute>
	</tag>
</taglib>

 

3. 在项目的web.xml目录中,加入paging标签的信息

 

  <jsp-config>  
    <taglib>  
    <taglib-uri>PagingTag</taglib-uri>  
    <taglib-location>/WEB-INF/paging.tld</taglib-location>  
    </taglib>  
  </jsp-config>  

 这个过程中发现项目下没有web.xml文件,之前写Servlet的时候就注意到了,去网上搜索,说是由于JSP3.0和Tomcat7的新特性,web.xml不是必需的,可以不要。至于Servlet的mapping,包含在其注释信息里面了(一直没找到在哪里。。)。需要生产web.xml文件,可以在新建项目的时候指定。See here

 

4. 现在基本上可以使用paging标签了,在jsp页面里面

<page:paging url="page.jsp" currentPage="${param.page}" 
                 pageCount="${param.pageCount}" 
                 params="p=${param.p}&pageCount=${param.pageCount}"></page:paging>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值