Jsp分页

百度的分页


 已知

   a:  中心页(center=第6个)

   b:  每页长度(size=10)

推理

   假设当前页:8

   顺序应该是:  3    4    5   6  7  [8]  9  10  11  12


   假设当前页:12

   顺序应该是:  7  8  9  10  11  [12] 13  14 15 16


   假设当前页: 7

   顺序应该是: 2  3  4  5  6  [7]  8  9 10 11


总结如下:

   那么起始页 =  (当前页-size)+1  , 结束页=当前页+(size-center);


servlet使用分页

		public String find(HttpServletRequest request,HttpServletResponse response)
		{
			List<Orgnizationno> orgnList = orgnizationnoService.find(orgnizationno);
			request.getSession().setAttribute("orgnizationnoList", orgnList);
			
			//回显第1页
			Paging<Orgnizationno> pag = new Paging<Orgnizationno>(orgnList,1);
			request.setAttribute("pag", pag);
			return "/WEB-INF/jsps/demo/books.jsp";
		}
		public String page(HttpServletRequest request,HttpServletResponse response) throws IOException
		{
			Object attri = request.getSession().getAttribute("orgnizationnoList");
			if(attri==null){
				response.sendRedirect("orgnizationno.do?method=find");
				return null;
			}
			//回显第n页
			List<Orgnizationno> orgnList = (List<Orgnizationno>) attri;
			Paging<Orgnizationno> pag = new Paging<Orgnizationno>(orgnList,orgnizationno.getThisPageNum());
			request.setAttribute("pag", pag);
			return "/WEB-INF/jsps/demo/books.jsp";
		}

jsp使用分页

<tr align="center">
  				<td colspan="3">
  					<a>当前${requestScope.pag.thisPageNum}页/共${requestScope.pag.totalPageNum}页</a>
  					<c:if test="${requestScope.pag.thisPageNum <= 1}">
	  					首页
	  					上一页
	  				</c:if>
	  				<c:if test="${requestScope.pag.thisPageNum > 1}">
	  					<a href="<c:url value="orgnizationno.do?method=page&thisPageNum=1"/>">首页</a>
	  					<a href="<c:url value="orgnizationno.do?method=page&thisPageNum=${requestScope.pag.thisPageNum-1}"/>">上一页</a>
	  				</c:if>
	  				
	  				<c:forEach begin="${requestScope.pag.startPageNum}" end="${requestScope.pag.endPageNum}" var="eee">
	  						<c:if test="${eee!=requestScope.pag.thisPageNum}">
	  							<a href="<c:url value="orgnizationno.do?method=page&thisPageNum=${eee}"/>">${eee}</a>
	  						</c:if>
	  						<c:if test="${eee==requestScope.pag.thisPageNum}">
					  			${eee}
				  			</c:if>	
	  				</c:forEach>
	  				<c:if test="${requestScope.pag.thisPageNum >= requestScope.pag.totalPageNum}">
		  				下一页
	  					未页	
	  				</c:if>
  					<c:if test="${requestScope.pag.thisPageNum < requestScope.pag.totalPageNum}">
  						<a href="<c:url value="orgnizationno.do?method=page&thisPageNum=${requestScope.pag.thisPageNum+1}"/>">下一页</a>
  						<a href="<c:url value="orgnizationno.do?method=page&thisPageNum=${requestScope.pag.totalPageNum}"/>">未页</a>
  					</c:if>
  				</td>  				
  			</tr>

下面帖出自己封装一个分页工具类

package com.apmbox.action.from;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hubiao 
 * @date  2014-08-10
 * 	  分页
 */
public class Paging<T> {
	private int thisPageNum;// 当前页码 = 页面传递
	private int entry = 8;// 每页显示几条 = 自定义
	private List<T> thisList;// 当前页码数据 = 当前页的数据集合
	private int totalPageNum;// 总页码 = 数据库总数据/每页显示的条数
	private int pageNum;// 当前页行号
	private int startPageNum;// 起始num
	private int endPageNum;// 结束num
	
	private int center = 5;//中心位置
	private int numberSize = 11;//页码长度
	
	public Paging() {
	}
	/**
	 * @param dataList  查询的条件数据
	 * @param thisPageNum  当前页
	 */
	public Paging(List<T> dataList,int thisPageNum) {
		this.thisPageNum = thisPageNum;
		
		//1:求出总页码
		totalPageNum = (dataList.size()%entry)==0?dataList.size()/entry:(dataList.size()/entry)+1;
		
		//2:求出每页的开始行号
		pageNum = ((thisPageNum*entry)-entry)+1;
		
		//3:数据结束位置
		int end = thisPageNum*entry;
		/*
		 * 	3:获取当前页数据:  
		 * 		起始位置从当前页*条数到  页码乘以条数结束。但可以整个数据集合长度可能小于pageNum*entry;
		 * 	thisPageNum = 2
		 * 	pageNum = (2*8)-8+1=9
		 *  end = 16 
		 * 	dataList = 13
		 * 	
		 * 	则从dataList索引为=9位置开始。到16. 而end只有13条,则应该在每次循环时加一个pageNum是否小于dataList总长度。
		 */
		thisList = new ArrayList<T>();
		for (int i = pageNum-1; i < end && i < dataList.size(); i++) {
			thisList.add(dataList.get(i));
		}
		
		//3:根据当前页码,求出起始最小页码
		startPageNum = getMin();
		
		//4:求出最大页码
		endPageNum = getMax();
	}
	private int getMax() {
		/**
		 * 	假设:页码长度为8;中间位置为4
		 * 	如下:
		 *      1 [2] 3  4   5  6  7  8	//当前页为2	结束长度为8 	
		 * 		2 3  4  [5]  6  7  8  9	//当前页为5	结束长度为9
		 * 		6 7  8  [9]  10 11 12 13  //当前页为9	结束长度为13
		 *		8 9  10 [11]  12 13 14 15  //当前页为11	结束长度为15
		 *	假设:页码长度为11;中间位置为5
		 * 	如下:
		 * 		[1]	2	3	4	5	 6	7	8	9	10	11	//当前页为:1	结束长度:11		
		 * 		3	4	5	6	[7]	 8	9	10	11	12	13	//当前页为:7	结束长度:13
		 * 		8	9	10	11	[12] 13	14	15	16	17	18	//当前页为:12	结束长度:13
		 * 		5	6	7	8	[9]  10  11  12 13  14  15 //当前页为:9	结束长度:15
		 *		1	2	3	4	[5]   6  7   8  9  10  11 //当前页为:5		结束长度:11
		 *		1	2	[3]	4	5   6  7   8  9  10  11 //当前页为:3		结束长度:11
		 *	推理出
		 *		结束页码=当前页码+(页码长度-中间位置)		
		 */
		/*如果当前页码小于  总页码长度,则返回总页码长度*/
		if(thisPageNum<=center)
			endPageNum =  numberSize;
		else
			endPageNum = thisPageNum+(numberSize-center);
		
		//如果总长度大于数据库长度,则结束位置就是数据库长度。
		if(endPageNum>=totalPageNum)
			endPageNum = totalPageNum;
		
		return endPageNum;
	}
	private int getMin() {
		//小于中心页。则直接第1页开始
		if(thisPageNum<=center)
			return 1;
		/**
		 * 	假设:页码长度为8;中间位置为4
		 * 		1	[2]	3	4	5	6	7	8	//当前页2	;起始页为1
		 * 		3	4	5	[6]	7	8	9	10	//当前页6	;起始页为10
		 * 		5	6	7	[8]	9	10	11	12	//当前页8	;起始页为12
		 * 		2	3	4	[5]	6	7	8	9	//当前页5	;起始页为8
		 * 	假设:页码长度为13;中间位置为7		
		 * 		[1]	2	3	4	5	6	7	8	9	10	11	12	13	//当前页1	;起始页为13
		 * 		2	3	4	5	6	7	[8]	9	10	11	12	13	14	//当前页8	;起始页为14
		 * 		4	5	6	7	8	9	[10]    11	12	13	14	15	16	//当前页10	;起始页为16
		 * 		3	4	5	6	7	8	[9]	10	11	12	13	14	15	//当前页9	;起始页为15
		 * 	推理:起始页=(当前页-中间页)+1
		 * 	如果小于中间位置,则直接从第1页开始
		 */
		return (thisPageNum-center)+1;
	}
	public int getNumberSize() {
		return numberSize;
	}
	public void setNumberSize(int numberSize) {
		this.numberSize = numberSize;
	}
	public int getCenter() {
		return center;
	}
	public void setCenter(int center) {
		this.center = center;
	}
	public int getThisPageNum() {
		return thisPageNum;
	}

	public void setThisPageNum(int thisPageNum) {
		this.thisPageNum = thisPageNum;
	}

	public int getEntry() {
		return entry;
	}

	public void setEntry(int entry) {
		this.entry = entry;
	}

	public List<T> getThisList() {
		return thisList;
	}

	public void setThisList(List<T> thisList) {
		this.thisList = thisList;
	}

	public int getTotalPageNum() {
		return totalPageNum;
	}

	public void setTotalPageNum(int totalPageNum) {
		this.totalPageNum = totalPageNum;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getStartPageNum() {
		return startPageNum;
	}

	public void setStartPageNum(int startPageNum) {
		this.startPageNum = startPageNum;
	}

	public int getEndPageNum() {
		return endPageNum;
	}

	public void setEndPageNum(int endPageNum) {
		this.endPageNum = endPageNum;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值