我的分页

我的分页有两个部分组成


这是我的分页类

PageList.java 

/**
 * 
 * 用于保存分页信息和负责分页逻辑运算的类,这个类配合标签使用最佳!
 * 这个类不是线程安全的
 * 
 * 
 * 使用方法: 
 * PagedList pList=new PagedList();
 * pList.setPageIndex();//当前页的页码
 * pList.setPageSize();//每页的大小
 * 
 * pList.setRowCount(dao.count(...));//获取数据库里的总记录数
 * pList.setList(dao.list(...));//从数据库获取当前页的数据
 * 
 * pList.addParam("category","5");//设置其他的url参数
 * request.setAttribute("pagedList",pList);//放入request
 * 至此,PagedList的数据填充就完成了,下一步是在JSP页面内做显示
 * 
 * 使用标签的时候,只需要这样:
 * 
 * 
 * @author Jonney
 */
public class PagedList {
	/**
	 * 当前页码
	 */
	private int pageIndex;

	/**
	 * 页大小
	 */
	private int pageSize;

	/**
	 * 页数量
	 */
	private int pageCount;

	/**
	 * 行数量
	 */
	private long rowCount;

	/**
	 * 目标页的uri
	 */
	private String uri;

	/**
	 * 数据列表
	 */
	private List list;

	private String paramEncoding = "utf-8";

	private List paramList = new ArrayList();

	/**
	 * 唯一的构造器,强制设置pageSize和pageIndex,这样才能保证setRowCount(int)不会出错
	 * 
	 * @param pageIndex
	 *            当前页码
	 * @param pageSize
	 *            页大小
	 */
	public PagedList(int pageIndex, int pageSize) {
		this.pageIndex = pageIndex < 1 ? 1 : pageIndex;	
		this.pageSize = pageSize;
	}

	//一些setter getter 方法

	/**
	 * 
	 * @param key
	 * @param value
	 */
	public void addParam(String key, String value) {
		if (key == null) {
			return;
		}
		if (value == null) {
			value = "";
		}
		Param p = new Param(key, value);
		paramList.add(p);
	}
 
	/**
	 * 
	 * @return
	 */
	public String getParams() {
		StringBuilder sb = new StringBuilder();
		try {
			for (Param p : paramList) {
				sb.append('&');
				sb.append(URLEncoder.encode(p.key, paramEncoding));
				sb.append('=');
				sb.append(URLEncoder.encode(p.value, paramEncoding));
			}
		} catch (UnsupportedEncodingException e) {
			// throw new RuntimeException(e);
		}
		return sb.toString();
	}

	/**
	 * 取当前页的第一行数据在数据库中的行号,在操作数据库的时候有用
	 * 
	 * @return
	 */
	public int getFirstRowInThisPage() {
		return (pageIndex - 1) * pageSize;
	}

	/**
	 * 是否第一页
	 * 
	 * @return
	 */
	public boolean isFirstPage() {
		return pageIndex <= 1 ? true : false;
	}

	/**
	 * 是否最后一页
	 * 
	 * @return
	 */
	public boolean isLastPage() {
		return pageIndex >= pageCount ? true : false;
	}

	private class Param {

		public String key;

		public String value;

		public Param(String key, String value) {
			this.key = key;
			this.value = value;
		}
	}

	public static void main(String[] args) {
		PagedList p = new PagedList(3, 4);
		p.addParam("1", "中文");
		p.addParam("2", "22");
		p.addParam("3", "33");
		System.out.println(p.getParams());
		p.setRowCount(40);
		System.out.println(p.getPageCount());
	}
}

 

  还有一个就是tag 也就是显示的样式

<%@ tag body-content="scriptless" pageEncoding="utf-8" isELIgnored="false"%>
<%@ attribute name="pagedList" required="true" type="generator.common.pagination.PagedList" %>
<%@ attribute name="pageIndexKey" required="false" type="java.lang.String" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:if test="${pageIndexKey==null}">
  <c:set var="pageIndexKey" value="pageIndex" />
</c:if>
<div id="page" class="pagination">
<table><tbody><tr>
<c:choose>
<c:when test="${pagedList.firstPage}">
<td><a href="javascript:void(0);"><img class="page-first" src="/images/crud/page-first-disabled.gif"></a></td>
</c:when>
<c:otherwise>
<c:url var="previousPage" value="${pagedList.uri}">
<c:param name="${pageIndexKey}" value="1"/>
</c:url>
<td><a href="${previousPage}${pagedList.params}"><img class="page-first" src="/images/crud/page-first.gif"></a></td>
</c:otherwise>
</c:choose>

<c:choose>
<c:when test="${pagedList.firstPage}">
<td><a href="javascript:void(0);"><img class="page-prev" src="/images/crud/page-prev-disabled.gif"></a></td>
</c:when>
<c:otherwise>
<c:url var="previousPage" value="${pagedList.uri}">
<c:param name="${pageIndexKey}" value="${pagedList.pageIndex-1}"/>
</c:url>
<td><a href="${previousPage}${pagedList.params}"><img class="page-prev" src="/images/crud/page-prev.gif"></a></td>
</c:otherwise>
</c:choose>

<td><span>第<input type="text" class="page-num" value="${pagedList.pageIndex}" size="2" onkeydown="if(event.keyCode==13) {window.location='${pagedList.uri}?${pageIndexKey}='+this.value+'${pagedList.params}'; return false;}" >页/共${pagedList.pageCount}页</span></td>

<c:choose>
<c:when test="${pagedList.lastPage}">
<td><a href="javascript:void(0);"><img class="page-next" src="/images/crud/page-next-disabled.gif"></a></td>
</c:when>
<c:otherwise>
<c:url var="nextPage" value="${pagedList.uri}">
<c:param name="${pageIndexKey}" value="${pagedList.pageIndex+1}"/>
</c:url>
<td><a href="${nextPage}${pagedList.params}"><img class="page-next" src="/images/crud/page-next.gif"></a></td>
</c:otherwise>
</c:choose>
<c:choose>

<c:when test="${pagedList.lastPage}">
<td><a href="javascript:void(0);"><img class="page-last" src="/images/crud/page-last-disabled.gif"></a></td>
</c:when>
<c:otherwise>
<c:url var="nextPage" value="${pagedList.uri}">
<c:param name="${pageIndexKey}" value="${pagedList.pageCount}"/>
</c:url>
<td><a href="${nextPage}${pagedList.params}"><img class="page-next" src="/images/crud/page-last.gif"></a></td>
</c:otherwise>
</c:choose>
<td><span style="padding-left:20px;">共 ${pagedList.rowCount} 记录</span></td>
</tr>
</tbody>
</table>
</div>

  

    然后你action里

   

   

private Integer page =1; //第几页
private Integer pageSize=20;//每页显示多少条
private PagedList<News> pagedList;

public void setPage(Integer page){
	this.page = page;
}

public PagedList<News> getPagedList(){
	return pagedList;
}


//业务代码
pagedList = new PagedList<News>(page, pageSize);

//pagedList.addParam("category","5");

newsProxy.fillNewsList(pagedList);

 

   proxy里代码

 

  

public void fillNewsList(PagedList<News> pagedList){
	pagedList.setRowCount(newsDao.count());
	pagedList.setList(newsDao.find(pagedList.getFirstRowInThisPage(),pagedList.getPageSize()));
}

 

   jsp代码

 

  

<ext:defaultPagination pagedList="${pagedList}" pageIndexKey="page"/>

 

 

 

    

   

你可能感兴趣的:(JavaScript,C++,c,jsp,C#)