1.分页工具类
package com.bw.shop.util; import java.util.List; import com.sun.org.apache.regexp.internal.recompile; public class PageModel { private List list; //结果集 private int totalRecords; //查询记录 private int pageSize; //每页记录条数 private int pageNo; //当前页数 private int totalPages; //总页数 private int previousPageNo; private int nextPageNo; private int bottomPage; public PageModel(){ } public PageModel(int pageNo,int pageSize,int totalRecords,List list){ this.pageNo = pageNo; this.pageSize = pageSize; this.totalRecords = totalRecords; this.list = list; } public int getTotalPages(){ if (totalRecords%pageSize == 0) totalPages = totalRecords / pageSize; else totalPages = totalRecords / pageSize + 1; return totalPages; } //获取首页 public int getTopPageNo(){ return 1; } //上一页 public int getPreviousPageNo(){ if (pageNo<=1) { previousPageNo = 1; }else { previousPageNo = pageNo - 1; } return previousPageNo; } //下一页 public int getNextPageNo() { if (pageNo >= getBottomPageNo()) { nextPageNo = getBottomPageNo(); } else { nextPageNo = pageNo + 1; } return nextPageNo; }
2.实现获取分页结果集和获取总的记录条数
//取得分页的结果集合 public List<Link> findByPage(int pageNo,int pageSize) { List<Link> list = new ArrayList<Link>(); String sql = "SELECT * FROM Link order by sort limit "+(pageNo-1)*pageSize+","+pageSize+" "; Connection conn = JdbcUtil.getConn(); PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(sql); rs = ps.executeQuery(); // 取得数据库端的查询结果集合 while (rs.next()) { // 每次判断是否有下一条数据,它从标题栏开始算 Link bean = new Link();// 每次取一行数据,就将该行数据 封装到Link对象中 bean.setLid(rs.getInt(1)); bean.setTitle(rs.getString(2)); bean.setImgpath(rs.getString(3)); bean.setUrl(rs.getString(4)); bean.setInfo(rs.getString(5)); bean.setIsshow(rs.getInt(6)); bean.setSort(rs.getInt(7)); // 将对象存入程序端的集合中 list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } JdbcUtil.closeAll(rs, ps, conn); return list; } //取得总的记录条数 public int getTotalRecords() { int total=0; String sql = "SELECT count(*) FROM Link "; Connection conn = JdbcUtil.getConn(); PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(sql); rs = ps.executeQuery(); // 取得数据库端的查询结果集合 if (rs.next()) { // 每次判断是否有下一条数据,它从标题栏开始算 total=rs.getInt(1); } } catch (SQLException e) { e.printStackTrace(); } JdbcUtil.closeAll(rs, ps, conn); return total; }
3.servlet操作
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置当前页数,默认是第1页 int pageNo =Integer.parseInt( request.getParameter("pageNo")==null?"1":request.getParameter("pageNo")); LinkService linkService=new LinkServiceImpl(); int totalRecords = linkService.getTotalRecords(); //取得分页的结果集合 List list = linkService.findByPage(pageNo, 3); //封装存值 PageModel pageModel = new PageModel(pageNo, 3, totalRecords, list); request.setAttribute("pageModel", pageModel); //跳转 request.getRequestDispatcher("linkList.jsp").forward(request, response); }
4.赋值
<TR class="td-block" align=middle> <TD height=19 width="100%" colSpan=4 align=right> <a href=${pageModel.pageNo==1?"#":"LinkServlet?pageNo=1"}> 首 页 </a> <a href="LinkServlet?pageNo=${pageModel.previousPageNo}"> 上一页 </a> <A href="LinkServlet?pageNo=${pageModel.nextPageNo}">下一页</A> <A href="LinkServlet?pageNo=${pageModel.bottomPageNo}">尾页</A> ${pageModel.pageNo}/${pageModel.totalPages} 页 <FONT color=red>${pageModel.pageSize}</FONT>条记录/页,共<FONT color=red>${pageModel.totalRecords}</FONT>条记录 <SELECT class="input" onchange=javascript:document.location='LinkServlet?pageNo='+this.value > <%PageModel pageModel = (PageModel) request.getAttribute("pageModel"); for (int i = 0; i < pageModel.getTotalPages(); i++) { out.println(" <OPTION "+((pageModel.getPageNo() == (i + 1)) ? "selected": "") + " value=" + (i + 1) + ">第"+ (i + 1) + "页</OPTION>"); }%> <INPUT value=1 type=hidden name=forward></TD></TR>