当前位置:文档之家› java web 分页技术详解及代码

java web 分页技术详解及代码

java web 分页技术详解及代码关于在java web上实现分页技术,方式实际上有很多,也各有个的特点,此处我只写些我的认识。

java web分页无外乎两种,一种是直接取出来,放到一个集合里,通过传begin 和end 参数控制分页,还有一种就是把分页工作交给数据库,让数据库读取需要的begin~end 之间的数据。

我们这里,先看从数据库中读取的情况操作数据库就需要tsql语句,mssqlserver2005新推出了一个row_number()很好用,还有就是mysql的limit也非常好使。

mssqlserver2005的如下:select * from (select row_number() over (order by ename) as rn, f.* from emp f) bwhere b.rn between 6 and 10;mysql的:select * from emp limit 5,5mysql的应注意,使用limit时,表中必须用主键,还有limit后的两个参数分别代表(标识位,长度),标识位从0开始现在开始一步步完成,首先完成model模块,建立pagebeanimport java.util.*;public class PageBean {private Collection objs;//从数据库中读的集合private int totalCount;//总的条数private int pageNo;//当前的页数private int pageCount;//每页的条数public int getPageCount() {return pageCount;}public void setPageCount(int pageCount) {this.pageCount = pageCount;}public void setPageNo(int pageNo) {this.pageNo = pageNo;}public int getTotalPage(){if(totalCount % pageCount == 0){return totalCount/pageCount;} else {return totalCount/pageCount + 1;}}//多写一个判断下一页的方法public boolean isNext(){return pageNo < getTotalPage();}//上一页的方法public boolean isPrevious(){return pageNo > 1;}public Collection getObjs() {return objs;}public void setObjs(Collection objs) {this.objs = objs;}public int getPageNo() {return pageNo;}public int getTotalCount() {return totalCount;}public void setTotalCount(int totalCount) {this.totalCount = totalCount;}public PageBean(Collection objs, int totalCount, int pageNo, int pageCount) {this.objs = objs;this.totalCount = totalCount;this.pageNo = pageNo;this.pageCount = pageCount;}}之后开始实现biz模块我们写一个具体分页的逻辑import java.util.*;import java.sql.*;public class EmpBiz {public EmpBiz() {}//具体实现分页的方法,传递两个参数,一个第几页,一个每页的数量public PageBean listEmps(int pageNo, int pageCount){Connection con = null;Statement stmt = null;ResultSet rs = null;ArrayList emps = new ArrayList();//次数我使用mssqlserver的方式,所以计算前后两个范围,用a,b表示int a = pageNo* pageCount;int b = (pageNo-1)* pageCount;try {Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Master;","sa" ,"sa");stmt = con.createStatement();rs = stmt.executeQuery("select * from (select row_number() over (order by ename) as rn, f.* from emp f) bwhere b.rn between "+a+" and "+b);while(rs.next()){Employee e = new Employee();e.setEmpno(rs.getInt("empno"));e.setEname(rs.getString("ename"));e.setSal(rs.getDouble("sal"));emps.add(e);}rs = stmt.executeQuery("select count(*) from emp");int totalCount=0;if(rs.next()){totalCount = rs.getInt(1);}PageBean pageBean = new PageBean(emps,totalCount,pageNo,pageCount);return pageBean;}catch (Exception ex) {System.out.println("发生错误,错误是:" + ex.getMessage());return null;} finally {if(stmt != null) {try { stmt.close(); } catch (SQLException ex1) {}}if(con != null) {try { con.close(); } catch (SQLException ex1) {}}}}}实现完EmpBiz之后,开始写servlet类import java.io.*;import java.util.*;public class EmpServletextends HttpServlet {private static final String CONTENT_TYPE = "text/html; charset=GBK";//Initialize global variablespublic void init() throws ServletException {}//Process the HTTP Get requestpublic void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType(CONTENT_TYPE);EmpBiz biz = new EmpBiz();//给页数和每页数量一个初始值int pageNo = 1;int pageCount = 5;String pageNoStr = request.getParameter("pageNo");String pageCountStr = request.getParameter("pageCount");if(pageNoStr != null){pageNo = Integer.parseInt(pageNoStr);}if(pageCountStr != null){pageCount = Integer.parseInt(pageCountStr);}PageBean pageBean = biz.listEmps(pageNo,pageCount);request.setAttribute("pageBean",pageBean);request.getRequestDispatcher("listemp.jsp").forward(request,response);}//Process the HTTP Post requestpublic void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}//Clean up resourcespublic void destroy() {}}业务逻辑完了之后,只要实现jsp页面即可<%@ page contentType="text/html; charset=GBK" %><%@ taglib uri="/jsp/jstl/core" prefix="c" %><html><head><title>listemp</title><style type="text/css">a {color:blue;text-decoration=underline;cursor:pointer;}</style><script type="text/javascript">function page(s){var myform = document.getElementByIdx("myform");var pageNo = document.getElementByIdx("pageNo");pageNo.value = s;myform.submit();}</script></head><body bgcolor="#ffffff"><h1>JBuilder Generated JSP</h1><form action="empservlet" method="POST" id="myform"><table border="1"><c:forEach items="${pageBean.objs}" var="t"><tr><td>${t.empno}</td><td>${t.ename}</td><td>${t.sal}</td></tr></c:forEach><tr><td colspan="3">跳转到:<input id="pageNo" type="text" name="pageNo" value="${pageBean.pageNo}" /> 每页记录数:<input type="text" name="pageCount" value="${pageBean.pageCount}" /> <input type="submit" value="跳转"/>共有${pageBean.totalPage}页<c:if test="${pageBean.previous}"><a onclick="page(${pageBean.pageNo - 1});">上一页</a></c:if><c:if test="${pageBean.next}"><a onclick="page(${pageBean.pageNo + 1});">下一页</a></c:if><a onclick="page(${pageBean.totalPage});">最后一页</a></td></tr></table> </form> </body> </html>。

相关主题