java分页

分页Model

public class PagingModel {

private Long curPage =1l;
private Long totalCount;
private Long totalPage;
private Long perPage = 20l;
private Long startRec;
private Long endRec;
private Long startPage;
private Long endPage;
private Long perStage = 5l;

public Long getCurPage() {
return curPage;
}

public void setCurPage(Long curPage) {
this.curPage = curPage;
}

public Long getTotalCount() {
if(this.totalCount == null){
this.totalCount = this.curPage * this.perPage;
}
return totalCount;
}

public void setTotalCount(Long totalCount) {
this.totalCount = totalCount;
}

public Long getTotalPage() {
return (long) Math.ceil((double)totalCount / (double)perPage);
}


public Long getPerPage() {
return perPage;
}

public Long getStartRec() {
return (this.curPage - 1) * this.perPage + 1;
}

public Long getEndRec() {

return Math.min(this.curPage * this.perPage, this.getTotalCount());
}

public Long getStartPage() {
return ((long) Math.ceil((double)this.curPage / (double)this.perStage)-1)
* this.perStage+1;
}

public Long getEndPage() {
return Math.min(((long) Math.ceil((double)this.curPage / (double)this.perStage)) * this.perStage, this.getTotalPage());
}

public Long getPerStage() {
return perStage;
}

@Override
public String toString() {
return "PagingObj [getCurPage()=" + getCurPage() + ", getTotalCount()="
+ getTotalCount() + ", getTotalPage()=" + getTotalPage()
+ ", getPerPage()=" + getPerPage() + ", getStartRec()="
+ getStartRec() + ", getEndRec()=" + getEndRec()
+ ", getStartPage()=" + getStartPage() + ", getEndPage()="
+ getEndPage() + ", getPerStage()=" + getPerStage() + "]";
}

}

前台页面分页标签:

<div id="paging-panel">
<table>
<tr>
<td>
                       Showing ${pagingObj.totalCount==0?'0':pagingObj.startRec } to ${pagingObj.endRec } of ${pagingObj.totalCount } entries
</td>
<td>
<button ${pagingObj.curPage==1?'disabled':'' } onclick="pagingAction(${1})">First</button>
<button ${pagingObj.curPage==1?'disabled':'' } onclick="pagingAction(${pagingObj.curPage-1})">Previous</button>
<c:if test="${pagingObj.startPage>pagingObj.perStage }">
<button onclick="pagingAction(${pagingObj.startPage-1})">...</button>
</c:if>
<c:forEach var="pageNum" begin="${pagingObj.startPage }" end="${pagingObj.endPage }" step="1"> 
<button style="background-color:${pagingObj.curPage==pageNum?'#777;':'' }" ${pagingObj.curPage==pageNum?'disabled':'' } onclick="pagingAction(${pageNum})">${pageNum }</button>
</c:forEach>
<c:if test="${pagingObj.totalPage>pagingObj.endPage }">
<button onclick="pagingAction(${pagingObj.endPage+1})">...</button>
</c:if>
<button ${(pagingObj.curPage==pagingObj.totalPage || pagingObj.totalPage==0)?'disabled':'' } onclick="pagingAction(${pagingObj.curPage+1})">Next</button>
<button ${(pagingObj.curPage==pagingObj.totalPage || pagingObj.totalPage==0)?'disabled':'' } onclick="pagingAction(${pagingObj.totalPage})">Last</button>
</td>
</tr>
</table>
</div>

function pagingAction(forwardPage){
document.checkertForm.action="queryQueue.do?curPage="+forwardPage;
document.checkertForm.submit();
}

后台Controller:

//pagination start

String curPageStr = request.getParameter("curPage");

if(curPageStr==null){
curPageStr = (String) request.getAttribute("curPage");
}

PagingModel pagingObj = new PagingModel();

//得到总记录条数
Long totalCount = this.exceptionQueueService.getTotalNum(userMap);
this.logger.debug("the size:"+totalCount);


pagingObj.setTotalCount(totalCount);
if(curPageStr != null){
Long curPage = Long.parseLong(curPageStr);
pagingObj.setCurPage(curPage);
}else{
pagingObj.setCurPage(1L);
}
request.setAttribute("pagingObj", pagingObj);

Long startRec = pagingObj.getStartRec();
Long endRec = pagingObj.getEndRec();

//把起始条数传到后台检索数据.
userMap.put("START", startRec.toString());
userMap.put("END", endRec.toString());

//pagination end

你可能感兴趣的:(java,分页)