PageBean.java
package entity;
import java.util.List;
/**
* @author DAJ
* @version 1.0
* @ClassName PageBean
* @date 2019/7/24 16:52
*/
public class PageBean {
private int totalCount; //总记录数
private int totalPage; //总页数
private List list; //每页的数据
private int currentPage; //当前页码
private int rows; //每页的记录数
public PageBean(int totalCount, int totalPage, List list, int currentPage, int rows) {
this.totalCount = totalCount;
this.totalPage = totalPage;
this.list = list;
this.currentPage = currentPage;
this.rows = rows;
}
public PageBean() {
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
@Override
public String toString() {
return "PageBean{" +
"totalCount=" + totalCount +
", totalPage=" + totalPage +
", list=" + list +
", currentPage=" + currentPage +
", rows=" + rows +
'}';
}
}
dao层
public int getTotalCount(Map map) {
String sql = "select count(*) from web4list where 1=1";
System.out.println("jt " + jt);
//1.定义StringBulider 用来拼接我们的sql语句
StringBuilder sb = new StringBuilder(sql);
//2.map的遍历
Set set = map.keySet();
//3.准备List集合以存储查询条件
List
serive
public PageBean getPageBean(String _currentPage, String _rows, Map map) {
//创建一个新的pageBean对象
PageBean pageBean = new PageBean<>();
int currentPage = Integer.parseInt(_currentPage);
int rows = Integer.parseInt(_rows);
pageBean.setCurrentPage(currentPage);
pageBean.setRows(rows);
//获得总记录数
int totalCount = dao.getTotalCount(map);
pageBean.setTotalCount(totalCount);
//获得总页数
int totalPage = totalCount % rows == 0 ? totalCount / rows : (totalCount / rows) + 1;
pageBean.setTotalPage(totalPage);
//传入 limit?,?;第一个?全部页码*页面大小
int start = (currentPage - 1) * rows;
if (currentPage > totalPage) {
currentPage = totalPage;
if (totalPage == 0) {
totalPage = 1;
} else {
start = (totalPage - 1) * rows;
}
}
List list = dao.getAthList(start, rows, map);
pageBean.setList(list);
return pageBean;
}
servlet
package servlet;
import entity.PageBean;
import entity.User;
import service.IPersonService;
import service.impl.IPersonServiceimpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
@WebServlet("/findUserByPageServlet")
public class FindUserByPageServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
IPersonService is = new IPersonServiceimpl();
//查询全部信息
// List list = is.selectALL();
// request.setAttribute("list", list);
// request.getRequestDispatcher("member-list.jsp").forward(request,response);
//分页查询
//获取当前的页数跟行数
String currenrPage = request.getParameter("currentPage");
String rows = request.getParameter("rows");
//判断页数为空时,显示第一页
if (currenrPage == null || "".equals(currenrPage)) {
currenrPage = "1";
} else if ("0".equals(currenrPage)) {
currenrPage = "1";
}
//设置一页显示的行数
if (rows == null || "".equals(rows)) {
rows = "5";
}
Map map = request.getParameterMap();
IPersonService serivce = new IPersonServiceimpl();
PageBean pageBean = serivce.getPageBean(currenrPage, rows, map);
//转发到前台界面进行展示
request.setAttribute("pb", pageBean);
request.setAttribute("map", map);
request.getRequestDispatcher("member-list.jsp").forward(request, response);
}
}
member-list.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
欢迎页面
编号
用户名
密码
性别
年龄
地址
状态
操作
${s.count}
${user.name}
${user.password}
${user.gender}
${user.age}
${user.address}
已启用
已拉黑
<%----%>
<%----%>
<%----%>
<%----%>
<%--
<%–<<–%>
<%––%>
<%–<%–1–%>–%>
<%–<%–2–%>–%>
<%–<%–3–%>–%>
<%–<%–489–%>–%>
<%–>>–%>
<%– –%>
< a href=" ">${i} a>
< a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${i}&rows=5&name=${condition.name[0]}&gender=${condition.gender[0]}&age=${condition.age[0]}&address=${condition.address[0]}">${i} a>
--%>