jsp数据显示分页
从数据库获得数据就不写了,直接写如何进行分页,为了方便,把方法和实体类都写到一个java文件里面了
这只是一种简单的分页方法,肯能会有比较多的bug
这是java里的构造实体类和方法,
1 public class PageUtilServices { 2 3 private int pageSize;//每页显示的条数 4 private int recordCount;//总共的条数 5 private int currentPage;//当前页面 6 //构造方法 7 public PageUtilServices(int pageSize, int recordCount, int currentPage) { 8 super(); 9 this.pageSize = pageSize; 10 this.recordCount = recordCount; 11 this.currentPage = currentPage; 12 } 13 public PageUtilServices() { 14 super(); 15 } 16 17 //总页数 18 public int getPageCount() { 19 int size = recordCount/pageSize;//总条数/每页显示的条数=总页数 20 int mod = recordCount % pageSize;//最后一页的条数 21 if(mod != 0) 22 size++; 23 return recordCount == 0 ? 1 : size; 24 } 25 //当前页的起始索引为0 26 public int getFromIndex() { 27 return (currentPage-1) * pageSize; 28 } 29 //当前页应展示的最后索引 30 public int getToIndex() { 31 return Math.min(recordCount, currentPage * pageSize); 32 } 33 34 public int getCurrentPage() { 35 return currentPage; 36 } 37 public void setCurrentPage(int currentPage) { 38 39 this.currentPage = currentPage; 40 } 41 public int getPageSize() { 42 return pageSize; 43 } 44 public void setPageSize(int pageSize) { 45 this.pageSize = pageSize; 46 } 47 public int getRecordCount() { 48 return recordCount; 49 } 50 public void setRecordCount(int recordCount) { 51 this.recordCount = recordCount; 52 } 53 }
这是jsp界面
<style type="text/css"> table{ margin: 0 auto; border-collapse:collapse; border-color: #666; } .lowstore{ color:red; } td{ text-align: center; } </style> </head> <body> <table border="1px"> <% BookInfoDao dao = new BookInfoDao(); List<Books> bookList = dao.showBooks(); String pageStr = request.getParameter("page"); int currentPage = 1; if (pageStr==null){ currentPage = 1; }else if(Integer.parseInt(pageStr)<=0){ currentPage = 1; }else if(Integer.parseInt(pageStr)>bookList.size()/3){ currentPage =bookList.size()/3+1; }else{ currentPage=Integer.parseInt(pageStr); } PageUtilServices pUtil = new PageUtilServices(3, bookList.size(), currentPage); currentPage = pUtil.getCurrentPage(); BookInfoDao bd=new BookInfoDao(); //ArrayList<Books> books=bd.showBooks(); if(bookList.isEmpty()==false){ %> <tr bgcolor="#eeeeee" colspan=13 align="center" > <td>编号</td> <td>名称</td> <td>作者</td> <td>价格</td> <td>发布日期</td> <td>库存</td> <td>操作</td> <td>修改</td> <td>入库</td> <td>出库</td> <td>评论</td> <td>查看评论</td> <td>购买图书</td> </tr> <% SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd"); //ArrayList<Books> bookList=bd.showBooks(); %> <a href="addbooks.jsp">添加图书</a><br/> <a href="searchbook.jsp">查找图书</a><br/> <a href="userlist.jsp?id=3">查看用户</a><br/> <a href="login.jsp">返回登录</a> <% for (int i = pUtil.getFromIndex(); i < pUtil.getToIndex(); i++) { Books book = bookList.get(i); %> <tr class=<%=bookList.get(i).getReserve()<10?"lowstore":""%>> <td><%=bookList.get(i).getBookId() %></td> <td><%=bookList.get(i).getBookName() %></td> <td><%=bookList.get(i).getAuther() %></td> <td><%=bookList.get(i).getPrice() %></td> <td><%=sf.format(bookList.get(i).getBookTime()) %></td> <td><%=bookList.get(i).getReserve() %></td> <td><a href="servlet/do_bookdelServlet?id=<%=bookList.get(i).getBookId() %>">删除</a></td> <td><a href="servlet/do_updatebookServlet?id=<%=bookList.get(i).getBookId() %>">修改</a></td> <td><a href="servlet/do_inbooksServlet?id=<%=bookList.get(i).getBookId() %>">入库</a></td> <td><a href="servlet/do_outbooksServlet?id=<%=bookList.get(i).getBookId() %>">出库</a></td> <td><a href="addcomment.jsp?id=<%=bookList.get(i).getBookId() %>">评论</a></td> <td><a href="servlet/do_showcomServlet?id=<%=bookList.get(i).getBookId() %>">查看评论</a></td> <td><a href="servlet/do_sellbookServlet?id=<%=bookList.get(i).getBookId() %>">购买</a></td> </tr> <% } }else{ %> <tr> <td>对不起,没有图书收录,请添加!</td> </tr> <a href="addbooks.jsp">添加</a><br/> <a href="login.jsp">返回登录</a> <% } %> <tr><td bgcolor="#eeeeee" colspan=13 align="center"> 记录总数<%=pUtil.getRecordCount()%>条 当前页/总页数<%=currentPage%> /<%=pUtil.getPageCount()%> 每页显示<%=pUtil.getPageSize()%>条 <a href="booklist.jsp?page=1">首页</a> <a href="booklist.jsp?page=<%=(currentPage - 1)%>">上页</a> <a href="booklist.jsp?page=<%=(currentPage + 1)%>">下页</a> <a href="booklist.jsp?page=<%=pUtil.getPageCount()%>">末页</a> </td></tr> </table> </body>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。