实现上面的效果,需要一个计算开始和结束索引的类PageIndex
public class PageIndex {
private Integer beginIndex;
private Integer endIndex;
public PageIndex(Integer beginPageIndex, Integer endPageIndex) {
this.beginIndex=beginPageIndex;
this.endIndex=endPageIndex;
}
public Integer getBeginIndex() {
return beginIndex;
}
public Integer getEndIndex() {
return endIndex;
}
/**
*
* @param totalIndexCount 总索引数
* @param currentPage 当前页
* @param totalPage 总页数
* @return
*/
public static PageIndex getPageIndex(Integer totalIndexCount,Integer currentPage,Integer totalPage) {
Integer startPageIndex=currentPage-(totalIndexCount%2==0?totalIndexCount/2-1 : totalIndexCount/2);
Integer endPageIndex=currentPage+totalIndexCount/2;
if(startPageIndex<1) {
startPageIndex=1;
if(totalPage>=totalIndexCount) {
endPageIndex=totalIndexCount;
}else {
endPageIndex=totalPage;
}
}
if(endPageIndex>totalPage) {
endPageIndex=totalPage;
if((endPageIndex-totalIndexCount)>0) {
startPageIndex=endPageIndex-totalIndexCount+1;
}else {
startPageIndex=1;
}
}
return new PageIndex(startPageIndex,endPageIndex);
}
}
在PageResult中增加两个字段:beginIndex和endIndex,并传入pageSize,currentPage计算出来
public class PageResult {
private List listData; //结果集数据,通过SQL查询
private Integer totalCount; //结果总条数,通过SQL查询
private Integer currentPage=1; //当前页:用户传入
private Integer pageSize; //每页条数:用户传入
private Integer beginPage=1; //首页
private Integer prevPage; //上页:计算出来
private Integer nextPage; //下页:计算出来
private Integer totalPage; //末页/总页数:计算出来
private Integer beginIndex;
private Integer endIndex;
private List<Integer> pageItems= Arrays.asList(2,3,4);
public Integer getBeginIndex() {
return beginIndex;
}
public Integer getEndIndex() {
return endIndex;
}
public List getPageItems() {
return pageItems;
}
public List getListData() {
return listData;
}
public Integer getTotalCount() {
return totalCount;
}
public Integer getCurrentPage() {
return currentPage;
}
public Integer getPageSize() {
return pageSize;
}
public Integer getBeginPage() {
return beginPage;
}
public Integer getPrevPage() {
return prevPage;
}
public Integer getNextPage() {
return nextPage;
}
public Integer getTotalPage() {
return totalPage;
}
public PageResult(List listData, Integer totalCount, Integer currentPage, Integer pageSize) {
this.listData = listData;
this.totalCount = totalCount;
this.currentPage = currentPage;
this.pageSize = pageSize;
//----------------------------------
this.totalPage = totalCount % pageSize==0?totalCount / pageSize:totalCount / pageSize+1;
this.prevPage = currentPage-1>=1 ? currentPage-1:1;
this.nextPage = currentPage+1<=totalPage ? currentPage+1 : totalPage;
//----------------------------------
PageIndex pageIndex=PageIndex.getPageIndex(pageSize, currentPage, totalPage);
this.beginIndex=pageIndex.getBeginIndex();
this.endIndex=pageIndex.getEndIndex();
}
public static PageResult empty(Integer pageSize) {
return new PageResult(new ArrayList(),0,1,pageSize);
}
}
前台增加跳转指定页面和每页显示数据数的功能,并通过js即时更新上传数据到后台
<script type="text/javascript">
function go(){
document.forms[0].submit();
}
</script>
<body>
<form action="page" method="post">
商品名称<input type="text" name="name" value="${qo.name }"><br>
商品价格<input type="text" name="minSalePrice" value="${qo.minSalePrice }">到<input type="text" name="maxSalePrice" value="${qo.maxSalePrice }">
<input type="submit" value="提交查询" style="background-color:blue">
<table border="1" cellpadding="0" cellspacing="0" width="90%">
<tr>
<th>货品编号</th>
<th>货品名称</th>
<th>货品品牌</th>
<th>货品分类</th>
<th>供 应 商</th>
<th>零 售 价</th>
<th>成 本 价</th>
<th>折 扣</th>
<th>操 作</th>
</tr>
<c:forEach items="${pageResult.listData}" var="p">
<tr>
<td>${p.id}</td>
<td>${p.productName}</td>
<td>${p.brand}</td>
<td>${p.dir_id}</td>
<td>${p.supplier}</td>
<td>${p.salePrice}</td>
<td>${p.cutoff}</td>
<td>${p.costPrice}</td>
<td>
<a href="#">编辑</a>
<a href="#">删除</a>
</td>
</tr>
</c:forEach>
<tr>
<td colspan="9" align="center">
<a href="page?currentPage=1">首页</a>
<a href="page?currentPage=${pageResult.prevPage}">上页</a>
<a href="page?currentPage=${pageResult.nextPage}">下页</a>
<c:forEach begin="${pageResult.beginIndex}" end="${pageResult.endIndex}" var="pageNumber">
<c:if test="${pageNumber!=pageResult.currentPage}">
<a href="page?currentPage=${pageNumber}">${pageNumber}</a>
</c:if>
<c:if test="${pageNumber==pageResult.currentPage}">
<span style="font:bold;">
${pageNumber}
</span>
</c:if>
</c:forEach>
<a href="page?currentPage=${pageResult.totalPage}">末页</a>
当前第${pageReault.currentPage}/${pageResult.totalPage}页,
一共${pageResult.totalCount}条数据
跳转到<input type="number" min="1" max="${pageResult.totalPage}" value="${pageResult.currentPage}" style="width:50px" name="currentPage"/>页
<input type="button" value="GO" onclick="go();">
每页
<select name="pageSize" onchange="go();" >
<c:forEach items="${pageResult.pageItems}" var="item">
<option ${item==pageResult.pageSize ? "selected" : "" }>${item} </option>
</c:forEach>
</select> 条数据
</td>
</tr>
</table>
</form>
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
Integer currentPage=1;
//接受用户传入的当前页
String sCurrentPage=req.getParameter("currentPage");
if(StringUtils.isNotBlank(sCurrentPage)) {
currentPage=Integer.valueOf(sCurrentPage);
}
Integer pageSize=3;
//接受用户传入的每页多少条
String sPageSize=req.getParameter("pageSize");
System.out.println(sPageSize);
if(StringUtils.isNotBlank(sPageSize)) {
pageSize=Integer.valueOf(sPageSize);
}
PageResult pageResult=dao.query(currentPage, pageSize);
req.setAttribute("pageResult", pageResult);
req.getRequestDispatcher("/WEB-INF/list.jsp").forward(req, resp);
}