1.增加用户等的快捷操作
可以使用工具类将用户信息封装好直接往后传递。jar包如下:链接:https://pan.baidu.com/s/1iJVufqwxonjdl8ujS6ecVg
提取码:hxa4
利用request.getParameterMap() 接收所有的参数,利用工具类,BeanUtils.populate(无参对象,parameterMap对象) 将数据传递。
2.分页展示所需要的数据
1.总页数
2.当前页数(前端传入)
3.每页条数(前端传入)
4.总条数
5.数据集合List
考虑到设置域对象一个一个比较麻烦,把这五个数据存储到一个类中,再放入到域对象更方便
limit n,m:n代表要跳过的条数,m代表每页需要查询的条数。
n=(currentPage-1)✖m
以下是分页小demo梳理
index.jsp
<a href="linkman?action=findByPage¤tPage=1&pageSize=2">分页查看联系人信息</a>
pageBean
public class PageBean implements Serializable {
private List<LinkMan> list;
//总数据条数
private Long totalSize;
//总页数
private Long totalPage;
//每页数据条数
private Integer pageSize;
//当前页数
private Long currentPage;
@Override
public String toString() {
return "PageBean{" +
"list=" + list +
", totalSize=" + totalSize +
", totalPage=" + totalPage +
", pageSize=" + pageSize +
", currentPage=" + currentPage +
'}';
}
public List<LinkMan> getList() {
return list;
}
public void setList(List<LinkMan> list) {
this.list = list;
}
public Long getTotalSize() {
return totalSize;
}
public void setTotalSize(Long totalSize) {
this.totalSize = totalSize;
}
public Long getTotalPage() {
return totalPage;
}
public void setTotalPage(Long totalPage) {
this.totalPage = totalPage;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Long getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Long currentPage) {
this.currentPage = currentPage;
}
}
分页servlet
//分页查询
private void findByPage(HttpServletRequest request, HttpServletResponse response) {
//当前页
Long currentPage= Long.valueOf(request.getParameter("currentPage"));
//每页个数
Integer pageSize= Integer.valueOf(request.getParameter("pageSize"));
//2.调用业务层的方法进行分页查询
PageBean pageBean= null;
try {
pageBean = linkManService.findByPage(currentPage,pageSize);
request.setAttribute("pageBean",pageBean);
request.getRequestDispatcher("list_page.jsp").forward(request,response);
} catch (SQLException e) {
e.printStackTrace();
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
分页service
public PageBean findByPage(Long currentPage, Integer pageSize) throws SQLException {
PageBean pageBean = new PageBean();
//设置5个属性
//当前页
pageBean.setCurrentPage(currentPage);
//每页个数
pageBean.setPageSize(pageSize);
//总个数
Long totalSize=linkManDao.totalSize();
pageBean.setTotalSize(totalSize);
//总页数
if(totalSize%pageSize==0){
pageBean.setTotalPage(totalSize/pageSize);
}else {
pageBean.setTotalPage((totalSize/pageSize)+1);
}
//展示的个数集合
List<LinkMan> list=linkManDao.findPageList(currentPage,pageSize);
pageBean.setList(list);
return pageBean;
}
分页dao
//查询总个数
public Long totalSize() throws SQLException {
String sql="select count(*) from linkman";
Long result= (Long) queryRunner.query(sql,new ScalarHandler());
return result;
}
//返回第几页几条数据的集合
public List<LinkMan> findPageList(Long currentPage,Integer pageSize) throws SQLException {
String sql="select * from linkman limit ?,?";
return queryRunner.query(sql,new BeanListHandler<>(LinkMan.class),(currentPage-1)*pageSize,pageSize);
}
前端展示界面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<!-- 网页使用的语言 -->
<html lang="zh-CN">
<head>
<!-- 指定字符集 -->
<meta charset="utf-8">
<!-- 使用Edge最新的浏览器的渲染方式 -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- viewport视口:网页可以根据设置的宽度自动进行适配,在浏览器的内部虚拟一个容器,容器的宽度与设备的宽度相同。
width: 默认宽度与设备的宽度相同
initial-scale: 初始的缩放比,为1:1 -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>Bootstrap模板</title>
<!-- 1. 导入CSS的全局样式 -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- 2. jQuery导入,建议使用1.9以上的版本 -->
<script src="js/jquery-2.1.0.min.js"></script>
<!-- 3. 导入bootstrap的js文件 -->
<script src="js/bootstrap.min.js"></script>
<style type="text/css">
td, th {
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<h3 style="text-align: center">显示所有联系人</h3>
<table border="1" class="table table-bordered table-hover">
<tr class="success">
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>籍贯</th>
<th>QQ</th>
<th>邮箱</th>
<th>操作</th>
</tr>
<c:forEach items="${pageBean.list}" var="linkman" varStatus="vst">
<tr>
<td>${vst.count}</td>
<td>${linkman.name}</td>
<td>${linkman.sex}</td>
<td>${linkman.age}</td>
<td>${linkman.address}</td>
<td>${linkman.qq}</td>
<td>${linkman.email}</td>
<td><a class="btn btn-default btn-sm" href="修改联系人.html">修改</a> <a class="btn btn-default btn-sm" href="修改联系人.html">删除</a></td>
</tr>
</c:forEach>
<%--新增一行,用于展示总数据条数以及每页数据条数--%>
<tr>
<td colspan="8" align="center">
总数据条数为${pageBean.totalSize}条<br>
每页显示${pageBean.pageSize}条数据
</td>
</tr>
<tr>
<td colspan="8" align="center">
<ul class="pagination success">
<%--
上一页,当前如果不是第一页才显示
--%>
<c:if test="${pageBean.currentPage != 1}">
<li>
<a href="linkman?action=findByPage¤tPage=${pageBean.currentPage - 1}&pageSize=${pageBean.pageSize}" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
</c:if>
<%--
页码:有多少页,就显示多少个页码,并且当前页的页码需要高亮显示
--%>
<c:forEach begin="1" end="${pageBean.totalPage}" var="i">
<%--
如果遍历的i就是当前页数,那么这一页应该高亮
--%>
<c:if test="${pageBean.currentPage == i}">
<%--
class="active"就是高亮
--%>
<li class="active"><a href="#">${i}</a></li>
</c:if>
<%--
如果遍历出来的i不是当前页数,那么这一页就不高亮
--%>
<c:if test="${pageBean.currentPage != i}">
<li><a href="linkman?action=findByPage¤tPage=${i}&pageSize=${pageBean.pageSize}">${i}</a></li>
</c:if>
</c:forEach>
<%--
下一页,如果当前页不是最后一页才显示
--%>
<c:if test="${pageBean.currentPage != pageBean.totalPage}">
<li>
<a href="linkman?action=findByPage¤tPage=${pageBean.currentPage + 1}&pageSize=${pageBean.pageSize}" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</c:if>
</ul>
</td>
</tr>
</table>
</div>
</body>
</html>
pageBean
此pageBean适用于所有的分页
package com.weijisheng.dao;
import java.io.Serializable;
import java.util.List;
public class PageBean<T> implements Serializable {
private List<T> list;
//总数据条数
private Long totalSize;
//总页数
private Long totalPage;
//每页数据条数
private Integer pageSize;
//当前页数
private Long currentPage;
@Override
public String toString() {
return "PageBean{" +
"list=" + list +
", totalSize=" + totalSize +
", totalPage=" + totalPage +
", pageSize=" + pageSize +
", currentPage=" + currentPage +
'}';
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public Long getTotalSize() {
return totalSize;
}
public void setTotalSize(Long totalSize) {
this.totalSize = totalSize;
}
public Long getTotalPage() {
return totalPage;
}
public void setTotalPage(Long totalPage) {
this.totalPage = totalPage;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Long getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Long currentPage) {
this.currentPage = currentPage;
}
}