page的model类:
public class Page { private Integer currentPage;//当前页 private int pageSize;//每页显示记录条数 private int totalPage;//总页数 private List<?> dataList;//每页显示的数据 private int star;//开始数据 public Integer getCurrentPage() { return currentPage; } public void setCurrentPage(Integer currentPage) { this.currentPage = currentPage; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public List<?> getDataList() { return dataList; } public void setDataList(List<?> dataList) { this.dataList = dataList; } public int getStar() { return star; } public void setStar(int star) { this.star = star; } }
控制层:
@RequestMapping("fenye.do") public String fen(Page page,HttpServletRequest request){ try { //查询出的list数据 List<Data> showdata=loginService.selectyichall(); //刚开始的页面为第一页 if (page.getCurrentPage() == null){ page.setCurrentPage(1); } else { page.setCurrentPage(page.getCurrentPage()); } //设置每页数据为十条 page.setPageSize(10); //每页的开始数 page.setStar((page.getCurrentPage() - 1) * page.getPageSize()); //list的大小 int count = showdata.size(); //设置总页数 page.setTotalPage(count % 10 == 0 ? count / 10 : count / 10 + 1); //对list进行截取 page.setDataList(showdata.subList(page.getStar(),count-page.getStar()>page.getPageSize()?page.getStar()+page.getPageSize():count)); //设置作用域 request.setAttribute("paging", page); return "add/show.jsp"; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return "mistake.jsp"; } }
jsp页面:
<script type="text/javascript" language="javascript"> function goPage(page){ location.href="fenye.do?currentPage="+page; } </script> <div> <table width="100%" border="0" align="center" cellspacing="1" > <c:forEach items="${paging.dataList }" var="upl"> <tr> <td align="center">${upl.name1 }</td> <td align="center">${upl.name2 }</td> <td align="center">${upl.name3 }</td> </tr> </c:forEach> </table> </div> <div style="text-align:center; margin-top:40px" id="venderfen"> 当前第${paging.currentPage }页/共${paging.totalPage }页 <c:choose> <c:when test="${paging.currentPage==1&&paging.totalPage==0}"> </c:when> <c:when test="${paging.currentPage==1&&paging.totalPage==1}"> </c:when> <c:when test="${paging.currentPage==2&&paging.totalPage==2}"> <input type="button" value="首页" onclick="goPage(1)" /> <input type="button" value="上一页" onclick="goPage(${paging.currentPage-1})" /> </c:when> <c:when test="${paging.currentPage==1 }"> <input type="button" value="上一页" > <input type="button" value="下一页" onclick="goPage(${paging.currentPage+1})" /> <input type="button" value="末页" onclick="goPage(${paging.totalPage})" /> </c:when> <c:when test="${paging.currentPage==paging.totalPage }"> <input type="button" value="首页" onclick="goPage(1)" /> <input type="button" value="上一页" onclick="goPage(${paging.currentPage-1})" /> </c:when> <c:otherwise> <input type="button" value="首页" onclick="goPage(1)" /> <input type="button" value="上一页" onclick="goPage(${paging.currentPage-1})" /> <input type="button" value="下一页" onclick="goPage(${paging.currentPage+1})" /> <input type="button" value="末页" onclick="goPage(${paging.totalPage})" /> </c:otherwise> </c:choose> </div>