java分页的实现(后台工具类和前台jsp页面)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java分页的实现(后台工具类和前台jsp页面)相关的知识,希望对你有一定的参考价值。
1、首先,新建一个类Page.java
1 public class Page implements Serializable { 2 private static final long serialVersionUID = -3198048449643774660L; 3 private int pageNow = 1; // 当前页数 4 private int pageSize = 10; // 每页显示记录的条数 5 private int totalCount; // 总记录条数 6 private int totalPageCount; // 总页数 7 private int startPos; // 开始位置,从0开始 8 //构造函数,参数:总记录数和当前页 9 public Page(int totalCount, int pageNow) { 10 this.totalCount = totalCount; 11 this.pageNow = pageNow; 12 } 13 //计算总页数,总页数=总记录数/每页显示记录的条数 14 public int getTotalPageCount() { 15 totalPageCount = getTotalCount() / getPageSize(); 16 return (totalCount % pageSize == 0) ? totalPageCount : totalPageCount + 1; 17 } 18 public void setTotalPageCount(int totalPageCount) { 19 this.totalPageCount = totalPageCount; 20 } 21 public int getPageNow() { 22 return pageNow; 23 } 24 public void setPageNow(int pageNow) { 25 this.pageNow = pageNow; 26 } 27 public int getPageSize() { 28 return pageSize; 29 } 30 public void setPageSize(int pageSize) { 31 this.pageSize = pageSize; 32 } 33 public int getTotalCount() { 34 return totalCount; 35 } 36 public void setTotalCount(int totalCount) { 37 this.totalCount = totalCount; 38 } 39 //取得选择记录的初始位置 40 public int getStartPos() { 41 return (pageNow - 1) * pageSize; 42 } 43 }
2、在Dao层加入两个方法,分别获取总的记录数和每页显示的list集合,DBUtil.java是数据库工具类。
1 // 获取总的记录数 2 public static int getCount() throws SQLException { 3 Connection conn = DBUtil.getConn(); 4 PreparedStatement pst = null; 5 String sql = "select count(*) from users"; 6 pst = conn.prepareStatement(sql); 7 ResultSet rs = pst.executeQuery(); 8 int count=0; 9 if(rs.next()){ 10 count=rs.getInt(1); 11 } 12 DBUtil.close(conn, pst, rs); 13 return count; 14 } 15 // 分页查找 16 public static ArrayList<Users> findByPage(int pos, int size) throws SQLException { 17 Connection conn = DBUtil.getConn(); 18 ArrayList<Users> userList = new ArrayList<Users>(); 19 PreparedStatement pst = null; 20 String sql = "select * from users limit ?,?"; 21 pst = conn.prepareStatement(sql); 22 pst.setInt(1, pos); 23 pst.setInt(2, size); 24 ResultSet rs = pst.executeQuery(); 25 while (rs.next()) { 26 Users user = new Users(); 27 user.setUserid(rs.getInt(1)); 28 user.setUsername(rs.getString(2)); 29 } 30 DBUtil.close(conn, pst, rs); 31 return userList; 32 }
3、在Servlet中调用Dao层的方法,并向页面传递参数,
1 String pageNow=request.getParameter("pageNow");//获取当前页 2 int totalCount=UsersDao.getCount();//获取总的记录数 3 List<Users> userList=new ArrayList<Users>(); 4 Page page=null; 5 if(pageNow!=null){ 6 page=new Page(totalCount, Integer.parseInt(pageNow)); 7 userList=UsersDao.findByPage(page.getStartPos(), page.getPageSize());//从startPos开始,获取pageSize条数据 8 }else{ 9 page=new Page(totalCount, 1);//初始化pageNow为1 10 userList=UsersDao.findByPage(page.getStartPos(), page.getPageSize());//从startPos开始,获取pageSize条数据 11 } 12 request.setAttribute("page", page); 13 request.setAttribute("userList", userList); 14 request.getRequestDispatcher("/WEB-INF/views/homePage.jsp").forward(request, response);
4、在jsp页面中添加如下代码:
1 <body> 2 <div class="page_nav"> 3 <c:choose> 4 <c:when test="${page.totalPageCount <= 10}"><!-- 如果总页数小于10,则全部显示 --> 5 <c:set var="begin" value="1"></c:set> 6 <c:set var="end" value="${page.totalPageCount }"></c:set> 7 </c:when> 8 <c:when test="${page.pageNow <= 5 }"><!-- 如果总页数小于5,则显示1-10页 --> 9 <c:set var="begin" value="1"></c:set> 10 <c:set var="end" value="10"></c:set> 11 </c:when> 12 <c:otherwise><!-- 否则,显示前5页和后5页,保证当前页在中间 --> 13 <c:set var="begin" value="${page.pageNow-5 }"></c:set> 14 <c:set var="end" value="${page.pageNow+5 }"></c:set> 15 <c:if test="${end > page.totalPageCount }"><!-- 如果end值小于总的记录数,则显示最后10页 --> 16 <c:set var="end" value="${page.totalPageCount}"></c:set> 17 <c:set var="begin" value="${end-10 }"></c:set> 18 </c:if> 19 </c:otherwise> 20 </c:choose> 21 <c:choose> 22 <c:when test="${page.pageNow != 1 }"><!-- 如果当前页为1,则不显示首页和上一页 --> 23 <a href="?oper=login&pageNow=1">首页</a> 24 <a href="?oper=login&pageNow=${page.pageNow-1 }">上一页</a> 25 </c:when> 26 </c:choose> 27 <!-- 遍历页码 --> 28 <c:forEach begin="${begin }" end="${end }" var="index"> 29 <c:choose> 30 <c:when test="${page.pageNow == index }"><!-- 如果为当前页,则特殊显示 --> 31 <a style="height:24px; margin:0 3px; border:none; background:#C00;">${index}</a> 32 </c:when> 33 <c:otherwise><!-- 否则,普通显示 --> 34 <a href="?oper=login&pageNow=${index }">${index }</a> 35 </c:otherwise> 36 </c:choose> 37 </c:forEach> 38 <c:choose> 39 <c:when test="${page.pageNow != page.totalPageCount }"><!-- 如果当前页为总的记录数,则不显示末页和下一页 --> 40 <a href="?oper=login&pageNow=${page.pageNow+1 }">下一页</a> 41 <a href="?oper=login&pageNow=${page.totalPageCount }">末页</a> 42 </c:when> 43 </c:choose> 44 共${page.totalPageCount }页,${page.totalCount }条记录 到第<input 45 value="${page.pageNow }" name="pn" id="pn_input" />页 <input 46 id="pn_btn" type="button" value="确定"> 47 <script type="text/javascript"> 48 //为按钮绑定一个单击响应函数 49 $("#pn_btn").click(function() { 50 //获取到要跳转的页码 51 var pageNow = $("#pn_input").val(); 52 //通过修改window.location属性跳转到另一个页面 53 window.location = "?oper=login&pageNow=" + pageNow; 54 }); 55 </script> 56 </div> 57 </body>
最后附上数据库工具类代码DBUtil:
1 public class DBUtil { 2 private static String DRIVER = null; 3 private static String URL = null; 4 private static String USERNAME = null; 5 private static String PASSWORD = null; 6 static { 7 DRIVER = "com.mysql.jdbc.Driver"; 8 URL = "jdbc:mysql://localhost:3306/test"; 9 USERNAME = "root"; 10 PASSWORD = "123456"; 11 } 12 public static Connection getConn() { 13 Connection conn = null; 14 try { 15 /* 加载驱动 */ 16 // System.out.println(DRIVER); 17 Class.forName(DRIVER); 18 /* 获取连接 */ 19 conn = DriverManager.getConnection(URL, USERNAME, PASSWORD); 20 21 } catch (ClassNotFoundException e) { 22 e.printStackTrace(); 23 } catch (SQLException e) { 24 e.printStackTrace(); 25 } 26 return conn; 27 } 28 // 关闭连接 29 public static void close(Connection conn, PreparedStatement ps, ResultSet rs) { 30 try { 31 if (rs != null) { 32 rs.close(); 33 } 34 if (ps != null) { 35 ps.close(); 36 } 37 if (conn != null) { 38 conn.close(); 39 } 40 } catch (SQLException e) { 41 e.printStackTrace(); 42 } 43 } 44 }
以上是关于java分页的实现(后台工具类和前台jsp页面)的主要内容,如果未能解决你的问题,请参考以下文章
今天要写一个java分页的页面,jsp页面需要传三个参数beginpage 和 endpage和当前的页数nowpage给action