JavaWeb分页技术
关于java web上实现分页技术,方式实际上有很多,也各有特点,此处我只写我的认识。Java web分页无外乎两种,一种直接取出来,放到一个集合了,通过传begin和end参数控制分页,还有一种就是把分页工作交给数据库,让数据库读取需要的begin和end直接的数据。
我使用的是后一种方式就是将分页的工作交给了数据库:我使用的是MySQL数据库
页面显示效果:
页面代码:
<form action="PageServlet" method="post" id="myform"> <table border="1"> <c:forEach items="${pageBean.arrayObjs }" var="obj"> <tr> <td>${obj }</td> </tr> </c:forEach> <tr> <td colspan="3"> 跳转到:<input id="currentPageNum" type="text" name="currentPageNum" value="${pageBean.currentPageNum }" /> 每页记录数:<input type="text" name="pageCount" value="${pageBean.pageCount }" /> <br /> <input type="submit" value="跳转" /> 共有${pageBean.totalPage } <c:if test="${pageBean.previous }"> <a onclick="page(${pageBean.currentPageNum}-1);">上一页</a> </c:if> <c:if test="${pageBean.next }"> <a onclick="page(${pageBean.currentPageNum}+1);">下一页</a> </c:if> <a onclick="page(${pageBean.totalPage});">最后一页</a> </td> </tr> </table> </form>
Servlet中的Post方法的代码:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub EmpBiz biz=new EmpBiz(); int currentPageNum=1; int pageCount=4; String currentPageNumStr=request.getParameter("currentPageNum"); String pageCountStr=request.getParameter("pageCount"); if(currentPageNumStr!=null){ currentPageNum=Integer.parseInt(currentPageNumStr); } if(pageCountStr!=null){ pageCount=Integer.parseInt(pageCountStr); } PageBean pageBean = biz.lisEmps(currentPageNum, pageCount); request.setAttribute("pageBean", pageBean); request.getRequestDispatcher("list.jsp").forward(request, response); }
在这里自己写了一个存储数据的类PageBean
package com.leejie.page.bean; import java.util.List; public class PageBean { private List arrayObjs; //从数据库中读出的集合 private int totalCount; //总条数 private int currentPageNum; //页数 private int pageCount; //每页的条数 public PageBean(List arrayObjs,int pageCount,int currentPageNum,int totalCount){ this.arrayObjs=arrayObjs; this.pageCount=pageCount; this.currentPageNum=currentPageNum; this.totalCount=totalCount; } public List getArrayObjs() { return arrayObjs; } public void setArrayObjs(List arrayObjs) { this.arrayObjs = arrayObjs; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public int getPageCount() { return pageCount; } public void setPageCount(int pageCount) { this.pageCount = pageCount; } public int getCurrentPageNum() { return currentPageNum; } public void setCurrentPageNum(int currentPageNum) { this.currentPageNum = currentPageNum; } public int getTotalPage(){ if(this.totalCount%this.pageCount==0){ return this.totalCount/this.pageCount; }else{ return this.totalCount/this.pageCount+1; } } public boolean isNext(){ return this.currentPageNum<this.getTotalPage(); } public boolean isPrevious(){ return this.currentPageNum>1; } }
写了一个数据访问层的类EmpBiz:
package com.leejie.page.biz; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.leejie.dbcon.util.DBCon; import com.leejie.page.bean.PageBean; public class EmpBiz { public EmpBiz(){ } public PageBean lisEmps(int currentPageNum,int pageCount){ List emps = new ArrayList(); int a = currentPageNum; int b = pageCount; String sql="select * from scores limit ?,?"; PreparedStatement ps=DBCon.getPreparedStatement(sql); try { ps.setInt(1, a); ps.setInt(2, b); ResultSet rs=ps.executeQuery(); int i=10; while(rs.next()){ emps.add(i); i+=5; } String sqlTotal="select count(*) from scores"; PreparedStatement psTotal = DBCon.getPreparedStatement(sqlTotal); int totalCount=0; ResultSet rsTotal=psTotal.executeQuery(); while(rsTotal.next()){ totalCount=rsTotal.getInt(1); } PageBean pageBean=new PageBean(emps, pageCount, currentPageNum, totalCount); return pageBean; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBCon.close(); } return null; } }
还需要用到一个访问数据库的工具类:DBCon,这里也贴出来了:
package com.leejie.dbcon.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class DBCon { private static final String DRIVERNAME="com.mysql.jdbc.Driver"; private static final String URL="jdbc:mysql://localhost:3306/school"; private static final String USERNAME="root"; private static final String PASSWORD="li257298"; private static Connection conn; private static PreparedStatement ps; static{ try { Class.forName(DRIVERNAME); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Connection getConnection() { try { conn=DriverManager.getConnection(URL,USERNAME,PASSWORD); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } public static PreparedStatement getPreparedStatement(String sql){ try { ps=getConnection().prepareStatement(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ps; } public static void close(){ if(ps!=null){ try { ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(conn!=null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }