思路
数据抽出
- 需要显示的数据,查询的数据抽出;
- 进行分页显示,需要统计抽出的件数,然后根据页面显示尺寸调整显示页面内容;
功能设计
- 翻页需要包含的内容:
- 首页/尾页
- 上一页/下一页
- 页码跳转,指定页跳转
- 需要有的参数:
- 当前页码
- 总页数
- 当前页所显示的件数
- 总件数
- 显示内容范围:当前页*页面件数
- 需要计算页面的件数和页数:
- 当前件数不满足页面内容,显示当前页
功能模块
工具类
- 定义基础参数
private int pageIndex = 1;
private int pageSize;
private int totalCount;
private int totalPageCount;
- 生成相关的getter、setter方法,调整相关方法的实现,赋值到时候一定要考虑到异常规避
public int getPageIndex() {return pageIndex;}public void setPageIndex(int pageIndex) {if (pageIndex > 0) {this.pageIndex = pageIndex;}}public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {if (pageSize > 0) {this.pageSize = pageSize;} else {this.pageSize = 10;}}public int getTotalCount() {return totalCount;}public void setTotalCount(int totalCount) {if (totalCount > 0) {this.totalCount = totalCount;setByPageNo(totalCount);}}public int getTotalPageCount() {return totalPageCount;}public void setTotalPageCount(int totalPageCount) {this.totalPageCount = totalPageCount;}
- 设置页数,在获取件数之后计算当前页数。需要防止非法字符越界等操作
private void setByPageNo(int totalCount) {if (this.pageSize <= 0) {this.pageSize = 10; }this.totalPageCount = totalCount % pageSize == 0 ? totalCount / pageSize :totalCount / pageSize + 1;}
前端内容
- 导入共同的翻页式样,设置hidden项:保存当前的页面件数
<div><table><li></li><li></li></table><input type="hidden" id="totalPageCount" value="${totalPageCount}"/><c:import url="rollpage.jsp"><c:param name="totalCount" value="${totalCount}"/><c:param name="currentPageNo" value="${currentPageNo}"/><c:param name="totalPageCount" value="${totalPageCount}"/></c:import>
</div>
- 调用的翻页jsp如下:文本输入框需要检查非法字符
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript"></script>
</head>
<body><div class="page-bar"><ul class="page-num-ul clearfix"><li>共${param.totalCount }条记录 ${param.currentPageNo }/${param.totalPageCount }页</li><c:if test="${param.currentPageNo > 1}"><a href="javascript:page_nav(document.forms[0],1);">首页</a><a href="javascript:page_nav(document.forms[0],${param.currentPageNo-1});">上一页</a></c:if><c:if test="${param.currentPageNo < param.totalPageCount }"><a href="javascript:page_nav(document.forms[0],${param.currentPageNo+1 });">下一页</a><a href="javascript:page_nav(document.forms[0],${param.totalPageCount });">最后一页</a></c:if> </ul><span class="page-go-form"><label>跳转至</label><input type="text" name="inputPage" id="inputPage" class="page-key" />页<button type="button" class="page-btn" onClick='jump_to(document.forms[0],document.getElementById("inputPage").value)'>GO</button></span></div>
</body>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/rollpage.js"></script>
</html>
- JS代码如下,检查非法字符
function page_nav(frm,num){frm.pageIndex.value = num;frm.submit();
}function jump_to(frm,num){const regexp = /^[1-9]\d*$/;let totalPageCount = document.getElementById("totalPageCount").value;if(!regexp.test(num)){alert("请输入大于0的正整数!");return false;}else if((num-totalPageCount) > 0){alert("请输入小于总页数的页码");return false;}else{page_nav(frm,num);}
}
用户端数据处理
- Servlet
private void query(HttpServletRequest req, HttpServletResponse resp) {int currentPageNo = 1;int pageSize = 5;String pageIndex = req.getParameter("pageIndex");currentPageNo = pageIndex == null ? currentPageNo : Integer.parseInt(pageIndex);UserService userService = new UserServiceImpl();List<User> userList;int totalCount = userService.getUserCount(queryUserName, queryUserRole);PageSupport pageSupport = new PageSupport();pageSupport.setPageIndex(currentPageNo);pageSupport.setPageSize(pageSize);pageSupport.setTotalCount(totalCount);int totalPageCount = pageSupport.getTotalPageCount();if (currentPageNo < 1) {currentPageNo = 1;} else if (currentPageNo > totalPageCount) {currentPageNo = totalPageCount;}userList = userService.getUserList(queryUserName, queryUserRole, currentPageNo, pageSize);req.setAttribute("userList", userList);req.setAttribute("totalCount", totalCount);req.setAttribute("currentPageNo", currentPageNo);req.setAttribute("totalPageCount", totalPageCount);req.getRequestDispatcher("/jsp/userlist.jsp").forward(req, resp);}
- Dao
StringBuilder sql = new StringBuilder();
sql.append("select u.*, r.roleName as userRoleName from smbms_user u, smbms_role r where " +"u.userRole = r.id");
sql.append(" order by createDate DESC limit ?, ?");
preparedStatement = connection.prepareStatement(sql.toString());
List<Object> list = new ArrayList<>();
currentPageNo = (currentPageNo - 1) * pageSize;
list.add(currentPageNo);
list.add(pageSize);
ResultSet rs = BaseDao.executeQuery(connection, String.valueOf(sql), preparedStatement, params, rs);while (rs.next()) {}