Springmvc 应用Mongodb分页实现(二)

2014-11-24 08:09:42 · 作者: · 浏览: 1
erride public int doStartTag() throws JspException { int half = size / 2; int point = size / 2 + size % 2; int start = 1; int loop = size; Pagination query = (Pagination) this.pageContext.getRequest().getAttribute(pageInfo); int pageSize = query.getPageSize(); int currentPage = query.getCurrentPage(); int totalPage = query.getTotalPage(); long totalNumber = query.getTotalNumber(); if(totalPage <= size){ start = 1; loop = totalPage; }else{ if(currentPage > point && currentPage < totalPage - half + 1){ start = 1 + (currentPage - point); }else if(currentPage > totalPage - half){ start = totalPage - size + 1; } } if(currentPage<=0){ currentPage = 1; } StringBuilder sb = new StringBuilder(); sb.append("
"); sb.append(totalNumber).append(" 条数据"); sb.append("/共 "); if(totalNumber!=0){ sb.append(totalPage).append(" 页!
    "); }else{ sb.append(0).append(" 页!
    "); } // 处理上一页 if(currentPage == 1){ sb.append("
  • 上一页
  • "); }else{ sb.append("
  • 上一页
  • "); } // 处理中间数据 for(int i = start; i < start + loop; i++){ //
  • 1
  • if(currentPage == i){ sb.append("
  • " + i + "
  • "); }else{ //
  • 2
  • sb.append("
  • " + i + "
  • "); } } // 处理下一页 if(currentPage == totalPage){ sb.append("
  • 下一页
  • "); }else{ sb.append("
  • 下一页
  • "); } sb.append("
"); sb.append(""); try { pageContext.getOut().write(sb.toString()); } catch (IOException e) { throw new JspException(e.toString(), e); } return super.doStartTag(); }

有上面的标签可以发现,每次点击页面页数的时候是用过js的dopage方法来实现的,该js方法根据form.submit()来提交信息查询信息(特别是currentPage数据)

js的代码如下

function dopage(currentPage,formid){
	$("#currentPage").val(currentPage);
	$("#" + formid).submit();
}

jsp页面中只需要添加上述标签即可(标签类Pagination以及转换成tld文件了―)