天天看点

jsp页面 jstl分页显示行号代码测试,

以前记得写了个,写的很长,不好理解,现在遇到了,随手写了个,就才几行代码而已!先留着

package test;

public class JstlPage {

	public static void main(String[] args) {
		int showNum = 10;
		int startR = 1;// 起始
		// 当前页
		int pags = 99; // 总页数
		
		// i==当前页
		for (int i = 0; i < pags; i++) {
			if (i % showNum == 0) {
				System.out.println("起始页" + i + ":翻页");
				System.out.println("\t显示的页码:");
				for (int j = i + 1; j < (i + 1 + showNum); j++) {
					System.out.print(" " + j);
				}
				System.out.println();
			}
		}
	}

}

/** output:
 * 起始页0:翻页
	显示的页码:
 1 2 3 4 5 6 7 8 9 10
起始页10:翻页
	显示的页码:
 11 12 13 14 15 16 17 18 19 20
起始页20:翻页
	显示的页码:
 21 22 23 24 25 26 27 28 29 30
起始页30:翻页
	显示的页码:
 31 32 33 34 35 36 37 38 39 40
起始页40:翻页
	显示的页码:
 41 42 43 44 45 46 47 48 49 50
起始页50:翻页
	显示的页码:
 51 52 53 54 55 56 57 58 59 60
起始页60:翻页
	显示的页码:
 61 62 63 64 65 66 67 68 69 70
起始页70:翻页
	显示的页码:
 71 72 73 74 75 76 77 78 79 80
起始页80:翻页
	显示的页码:
 81 82 83 84 85 86 87 88 89 90
起始页90:翻页
	显示的页码:
 91 92 93 94 95 96 97 98 99 100

 */
           

分页java控制实现

package com.crm.mess.bean.pager;


import java.util.List;


public class QueryResult<T> {
<span style="white-space:pre">	</span>private int mPages; // 总页数
<span style="white-space:pre">	</span>private int mRecords;// 总记录数
<span style="white-space:pre">	</span>private List<T> mItems;
<span style="white-space:pre">	</span>private int mCurrentPage = 1;// 当前页


<span style="white-space:pre">	</span>private int startNo;// 起始号


<span style="white-space:pre">	</span>private int showNum = 5;// 显示号的数量


<span style="white-space:pre">	</span>public int getmPages() {
<span style="white-space:pre">		</span>return mPages;
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>public void setmPages(int mPages) {
<span style="white-space:pre">		</span>this.mPages = mPages;
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>// 计算总页数
<span style="white-space:pre">	</span>public void setmPages(int mRecords, int mpageSize) {
<span style="white-space:pre">		</span>this.mRecords = mRecords;


<span style="white-space:pre">		</span>if (mRecords <= 0) {
<span style="white-space:pre">			</span>this.mPages = 1;
<span style="white-space:pre">			</span>return;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>if (mRecords <= mpageSize) {
<span style="white-space:pre">			</span>this.mPages = 1;
<span style="white-space:pre">			</span>return;
<span style="white-space:pre">		</span>}


<span style="white-space:pre">		</span>long totalPages = mRecords / mpageSize;
<span style="white-space:pre">		</span>this.mPages += (totalPages + (mRecords % mpageSize == 0 ? 0 : 1));


<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>public int getmRecords() {
<span style="white-space:pre">		</span>return mRecords;
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>public void setmRecords(int mRecords) {
<span style="white-space:pre">		</span>this.mRecords = mRecords;
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>public List<T> getmItems() {
<span style="white-space:pre">		</span>return mItems;
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>public void setmItems(List<T> mItems) {
<span style="white-space:pre">		</span>this.mItems = mItems;
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>public int getmCurrentPage() {


<span style="white-space:pre">		</span>if (mCurrentPage == 0) {
<span style="white-space:pre">			</span>mCurrentPage = 1;
<span style="white-space:pre">		</span>}


<span style="white-space:pre">		</span>return mCurrentPage;
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>public void setmCurrentPage(int mCurrentPage) {
<span style="white-space:pre">		</span>this.mCurrentPage = mCurrentPage;
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>public int getStartNo() {
<span style="white-space:pre">		</span>if (startNo <= 0) {
<span style="white-space:pre">			</span>return 1;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>return startNo;
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>public void setStartNo(int startNo) {


<span style="white-space:pre">		</span>// 当前页%显示的数量==是否翻页
<span style="white-space:pre">		</span>if (getmCurrentPage() % showNum == 1) {
<span style="white-space:pre">			</span>if (getmPages() <= startNo) {
<span style="white-space:pre">				</span>this.startNo = getmPages() - showNum;
<span style="white-space:pre">			</span>} else {
<span style="white-space:pre">				</span>this.startNo = getmCurrentPage();//+ 1;
<span style="white-space:pre">			</span>}


<span style="white-space:pre">		</span>} else if (getmCurrentPage() % showNum == 0) {
<span style="white-space:pre">			</span>this.startNo = getmCurrentPage() - showNum;
<span style="white-space:pre">		</span>} else {
<span style="white-space:pre">			</span>this.startNo = startNo;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>if (startNo <= 0||this.startNo<=0) {
<span style="white-space:pre">			</span>this.startNo = 1;
<span style="white-space:pre">		</span>}


<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>public int getShowNum() {
<span style="white-space:pre">		</span>return showNum;
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>public void setShowNum(int showNum) {
<span style="white-space:pre">		</span>this.showNum = showNum;
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>@Override
<span style="white-space:pre">	</span>public String toString() {
<span style="white-space:pre">		</span>return "QueryResult [mPages=" + mPages + ", mRecords=" + mRecords
<span style="white-space:pre">				</span>+ ", mItems=" + mItems + ", mCurrentPage=" + mCurrentPage
<span style="white-space:pre">				</span>+ ", startNo=" + startNo + ", showNum=" + showNum + "]";
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>public static void main(String[] args) {
<span style="white-space:pre">	</span> 
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>int i=5;
<span style="white-space:pre">		</span>System.out.println(10%5);
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>


}
           

这里可以再做一个对象控制,这里省略

// 查询数据
			if (page <= 0) {
				page = 1;
			}
			if (pages.getmPages() < page) {
				page = pages.getmPages();

			}
           

页面显示

<!-- 分页显示  -->
		<div class="pagination">
			<%-- 			<span class="pageFl prev" rel="prev"	οnclick="pageFn(${pages.mCurrentPage-1})">< </span> --%>

			<span class="next" rel="prev"
				οnclick="pageFn(${pages.mCurrentPage-1})">< </span>
			<c:forEach var="pageNo" begin="${pages.startNo}"
				end="${pages.startNo+pages.showNum-1}" step="1" varStatus="i">
				<c:if test="${pageNo<=pages.mPages}">
					<a href="javascript:pageFn(${pageNo})" target="_blank" rel="external nofollow" 
						<c:if test="${pageNo==pages.mCurrentPage}"> class='pageSelected' </c:if>>
						${pageNo} </a>
				</c:if>
			</c:forEach>
			<script type="text/javascript">
				function pageFn(pageNo) {
					//alert(pageNo);
					$("#_page_id").val(pageNo);
					srachSubFn();
				}
			</script>

			<span class="next" rel="prev"
				οnclick="pageFn(${pages.mCurrentPage+1})"> > </span>

		</div>
           

=================================================================================================================

以前公司通用的js 异步分页代码,有点长

function createPager(ajaxMethod, id, pageSize)
{
	if (pageSize == undefined)
	{
		pageSize = 20;
	}
	var html = '<div class="xyt-pager" id="pager_'
			+ ajaxMethod
			+ '">\
    			<div class="xyt-pager-content">\
	<span id="'
			+ ajaxMethod
			+ '_first" class="disabled" οnclick="ajaxPagerNav(\''
			+ ajaxMethod
			+ '\',\'F\')">首页</span>\
        			<span id="'
			+ ajaxMethod
			+ '_prev" class="xyt-pager-left disabled opacity" title="上一页" οnclick="ajaxPagerNav(\''
			+ ajaxMethod
			+ '\',\'P\')"></span>\
        			<span class="default">第 <input type="text" size="5" maxLength="10" id="'
			+ ajaxMethod
			+ '_currentPage" value="0" οnkeypress="event.returnValue=((event.keyCode >= 48) && (event.keyCode <= 57));if(event.keyCode==13){if(!isPos(this.value)){this.value=1;}else{ajaxPagerNav(\''
			+ ajaxMethod
			+ '\',\'C\');}}"/> / <span id="'
			+ ajaxMethod
			+ '_totalPage">0</span> 页</span>\
        			<span class="default">\
        				每页 <span id="'
			+ ajaxMethod
			+ '_pageSize">0</span> 条记录\
        				共 <span id="'
			+ ajaxMethod
			+ '_dataSize">0</span> 条记录\
        			</span>\
        			<span id="'
			+ ajaxMethod
			+ '_next" class="xyt-pager-right disabled opacity" title="下一页" οnclick="ajaxPagerNav(\''
			+ ajaxMethod
			+ '\',\'N\')"></span>\
        			<span id="'
			+ ajaxMethod
			+ '_last" class="disabled" οnclick="ajaxPagerNav(\''
			+ ajaxMethod
			+ '\',\'L\')">末页</span>\
        		</div>\
        		<input type="hidden" name="pageSize" value="'
			+ pageSize
			+ '"/>\
        		<input type="hidden" name="currentPage" value="1"/>\
        	 </div>';
	if (id == undefined)
	{
		document.writeln(html);
	} else
	{
		$("#" + id).html(html);
	}
}

function ajaxPagerNav(ajaxMethod, nav)
{
	//10进制
	var dataSize = parseInt($("#" + ajaxMethod + "_dataSize").html(), 10);
	var pageSize = parseInt($("#pager_" + ajaxMethod + ">[name='pageSize']")
			.val(), 10);
	var currentPage = parseInt($("#" + ajaxMethod + "_currentPage").val(), 10);
	var totalPage = 0;
	if (dataSize < pageSize)
	{
		totalPage = 1;
	} else
	{
		totalPage = dataSize / pageSize;
		totalPage += dataSize % pageSize > 0 ? 1 : 0;
	}
	totalPage = parseInt(totalPage + "", 10);
	if (nav == "P")
	{
		if (currentPage <= 1)
		{
			return;
		}
		currentPage -= 1;
	} else if (nav == "F")
	{
		if (currentPage <= 1)
		{
			return;
		}
		currentPage = 1;
	} else if (nav == "N")
	{
		if (currentPage >= totalPage)
		{
			return;
		}
		currentPage += 1;
	} else if (nav == "L")
	{
		if (currentPage >= totalPage)
		{
			return;
		}
		currentPage = totalPage;
	} else if (nav == "C")
	{
		if (currentPage > totalPage || currentPage < 1)
		{
			return;
		}
	}
	$("#pager_" + ajaxMethod + ">[name='currentPage']").val(currentPage);
	eval(ajaxMethod + "()");
}

function setAjaxPagerValues(ajaxMethod, dataSize, pageSize, currentPage)
{
	var totalPage = 0;
	if (dataSize < pageSize)
	{
		totalPage = 1;
	} else
	{
		totalPage = dataSize / pageSize;
		totalPage += dataSize % pageSize > 0 ? 1 : 0;
	}
	totalPage = parseInt(totalPage + "", 10);
	if (dataSize == 0)
	{
		currentPage = 1;
	}
	if (currentPage > totalPage)
	{
		currentPage = 1;
	}
	$("#" + ajaxMethod + "_currentPage").val(currentPage);
	$("#" + ajaxMethod + "_totalPage").html(totalPage);
	$("#" + ajaxMethod + "_dataSize").html(dataSize);
	$("#" + ajaxMethod + "_pageSize").html(pageSize);
	$("#pager_" + ajaxMethod + ">[name='pageSize']").val(pageSize);
	$("#pager_" + ajaxMethod + ">[name='currentPage']").val(currentPage);
	$("#" + ajaxMethod + "_first").attr("class",
			currentPage > 1 ? "" : "disabled");
	$("#" + ajaxMethod + "_last").attr("class",
			currentPage == totalPage ? "disabled" : "");
	$("#" + ajaxMethod + "_prev").attr(
			"class",
			currentPage > 1 ? "xyt-pager-left"
					: "xyt-pager-left disabled opacity");
	$("#" + ajaxMethod + "_next").attr(
			"class",
			currentPage == totalPage ? "xyt-pager-right disabled opacity"
					: "xyt-pager-right");
}
           

java 分页代码 改日奉上