ajax 分页改成直接url总结

1.jquery下ajax分页的
<div class="pagination pagination-right pagination-small">
	<ul>
	    <li>
			<c:if test="${page.pageNo>1 }">
				<a href="${pageControl.url}&page.pageNo=${page.pageNo-1}" >《上一页</a>
			</c:if>
	    </li>
	    <c:if test="${pageControl.start>1 }">
	    	<li>
				<a href="${pageControl.url}&page.pageNo=1" class="number" >1</a>
			</li>
    		<li>
				<span>...</span>
			</li>
		</c:if>
	    <c:forEach begin="${pageControl.start }" end="${pageControl.end }" var = "i"> 
			<c:choose>
				<c:when test="${page.pageNo eq i}">
				    <li class="active">
						<a href="${pageControl.url}&page.pageNo=${i}" class="number">${i }</a>
					</li>
				</c:when>
				<c:otherwise>
					 <li class="disabled">
						<a href="${pageControl.url}&page.pageNo=${i}" class="number">${i }</a>
					</li>
				</c:otherwise>
			</c:choose>
		</c:forEach>
		<c:if test="${pageControl.end<page.totalPages}">
			<li>
				<span>...</span>
			</li>
    		<li>
				<a href="${pageControl.url}&page.pageNo=${page.totalPages}" class="number">${page.totalPages }</a>
			</li>
		</c:if>
	    <li>
			<c:if test="${page.pageNo <page.totalPages }">
				<a href="${pageControl.url}&page.pageNo=${page.pageNo+1}" >下一页》</a>
			</c:if>
		</li>
	  </ul>
</div>


对应的js调用如下
function pageTurning(queryString, pageNo) {
		$.ajax({
			type : 'GET',
			url : '${ctx}/pregnancy-personal!search?' + queryString
					+ '&page.pageNo=' + pageNo,
			success : function(data) {
				$("#search-result").html(data);
			}
		});
		return false;
	}

	function Pagination(queryString, i) {
		$.ajax({
			type : 'GET',
			url : '${ctx}/pregnancy-personal!search?' + queryString
					+ '&page.pageNo=' + i,
			success : function(data) {
				$("#search-result").html(data);
			}
		});
		return false;
	}


queryString代码如下
queryString = ServletActionContext
				.getRequest().getQueryString();


PageControl的控制类代码如下
public class PageControl {

	private String url;
	
	private int start;
	
	private long end;
	
	/**
	 * 根据当前页码,总页数构建开始和结束显示页码数
	 * @param pageNo	当前显示的页
	 * @param totalPage	总页数
	 */
	public void buildPageLoop(int pageNo, long totalPage){
		// avgPageCount 要显示的页数的平均总数  
		// 总页数大于想要显示的页数
		int avgPageCount = 5;
		
		if (totalPage > avgPageCount) {
			// 当前页大于要显示的中间页
			if (pageNo > avgPageCount / 2 + 1) {
				// 当前页是后面的x条记录时
				if (pageNo > totalPage - (avgPageCount / 2 + 1)) {
					this.start = pageNo - (avgPageCount / 2 + 1);
					this.end = totalPage;
				} else {
					this.start = pageNo - (avgPageCount / 2 + 1);
					this.end = pageNo + (avgPageCount / 2 + 1);
				}
			} else {
				// 当前页小于最小的中间页5的时候,只需设置其范围为1——pageNo+avgPageCount/2+1
				this.start = 1;
				this.end = pageNo + avgPageCount / 2 + 1;
			}
		} else {
			this.start = 1;
			this.end = totalPage;
		}
	}
	
	/**
	 * 根据请求构建分页的url内容
	 * @param request
	 */
	public void buildPageUrl(HttpServletRequest request) {
		String queryString = ActionUtils.buildQueryString(ServletActionContext
				.getRequest());
		String url = ServletActionContext.getRequest().getRequestURI().toString();
		if(url.indexOf("!search") >0) {
			//搜索页面的页面跳转
			url = url +"?"+queryString;
		}else{
			//首页的页面跳转
			url = url.substring(0, url.length()-1) + "!search?" + queryString;
		}
		this.url = url;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public long getEnd() {
		return end;
	}

	public void setEnd(long end) {
		this.end = end;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

你可能感兴趣的:(Ajax)