实现分页功能 web
2015-09-14 16:59
281 查看
首先新建实现分页工具类:
在数据访问类中添加一个方法 来获得数据的总条数
在数据处理类中来处理分类:
在jsp页面中使用javaScript来做处理:
package com.servlet.product.util; public class DividePageUtil { private int pageSize;// 表示显示的条数 private int recordCount;// 表示记录的总条数 private int currentPage;// 表示当前页 public DividePageUtil(int pageSize, int recordCount, int currentPage) { // TODO Auto-generated constructor stub this.pageSize = pageSize; this.recordCount = recordCount; setCurrentPage(currentPage); } public DividePageUtil(int pageSize, int recordCount) { // TODO Auto-generated constructor stub this(pageSize, recordCount, 1); } // 获得总页数 public int getPageCount() { int size = recordCount / pageSize; int mod = recordCount % pageSize; if (mod != 0) { size++; } return recordCount == 0 ? 1 : size; } public int getFromIndex() { return (currentPage - 1) * pageSize; } public int getToIndex() { return pageSize; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { int validPage = currentPage <= 0 ? 1 : currentPage; validPage = validPage > getPageCount() ? getPageCount() : validPage; this.currentPage = validPage; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getRecordCount() { return recordCount; } public void setRecordCount(int recordCount) { this.recordCount = recordCount; } }
在数据访问类中添加一个方法 来获得数据的总条数
public int getItemCount() { int result = 0; Map<String, Object> map = null; String sql = " select count(*) mycount from product "; try { conn.getConnect(); map = conn.queryBySimpleResult(sql, null); result = Integer.parseInt(map.get("mycount").toString()); } catch (SQLException e) { e.printStackTrace(); } finally { conn.releaseConnect(); } return result; }
在数据处理类中来处理分类:
// 获得数据总条数 int recordCount = service.getItemCount(); // 当前页面 初始化为 1 int currentPage = 1; // 获得当前页面 String pageNum = request.getParameter("pageNum"); if (pageNum != null) { currentPage = Integer.parseInt(pageNum); } DividePageUtil util = new DividePageUtil(5, recordCount, currentPage); //开始的条数 int start = util.getFromIndex(); //结束的条数 int end = util.getToIndex(); String proname = (String) request.getParameter("proname"); List<Map<String, Object>> list = service.listProduct(proname, start, end); request.setAttribute("util", util);
在jsp页面中使用javaScript来做处理:
function firstPage(){ var th=document.form1; th.action="<%=path %>/servlet/ProductAction?action_flag=list&pageNum=1"; th.submit(); } function forward(){ var th=document.form1; th.action="<%=path %>/servlet/ProductAction?action_flag=list&pageNum=<%=util.getCurrentPage()-1%>"; th.submit(); } function next(){ var th=document.form1; th.action="<%=path %>/servlet/ProductAction?action_flag=list&pageNum=<%=util.getCurrentPage()+1%>"; th.submit(); } function endPage(){ var th=document.form1; th.action="<%=path %>/servlet/ProductAction?action_flag=list&pageNum=<%=util.getPageCount()%>"; th.submit(); } function onChange(currentPage){ var th=document.form1; th.action="<%=path %>/servlet/ProductAction?action_flag=list&pageNum="+currentPage; th.submit();
相关文章推荐
- NSString 字符串替换
- 前App Store高管揭秘:关于“苹果推荐”的七大真相
- List
- Java学习-030-JSON 之四 -- 判断 JSONObject 是否包含键值对
- 第二周 项目1 函数参数传递的三种方式
- ffmpeg 读取MPEG-TS流,切割TS流支持【HLS协议】
- Java连接六类数据库技巧全攻略
- Linux静态库与动态库的详解
- 排列组合算法
- 字符串循环移位
- (转)cacti无图无数据等常见问题排查
- boost compile
- could not find implicit value for parameter extractor 问题的解决
- 第二周项目二程序的多文件组织
- framework not found XCTest
- Android Studio二--基本设置与运行
- 翻转字符串
- Java String 判断为空
- javaScript & jquery完美判断图片是否加载完毕
- Android Canvas绘图详解(图文)