标签:jsp ring std orm title start ext parameter hidden
分页的实现分为真分页和假分页两种。
1.真分页(物理分页):
SELECT * FROM xxx [WHERE...] LIMIT ?, 10;
2.假分页(逻辑分页):
分页的一些术语:
				-- 数据总条数: totalCount : select count(1) from t_user;
				-- 每页显示条数:pageSize
				-- 总页数:totalPage
				-- 当前页:currPage
				-- 起始索引: startIndex
-- 通过当前页码查询第几页的数据
select * from t_user limit 0, 5; -- 页码 1
select * from t_user limit 5, 5; -- 页码 2
select * from t_user limit 10, 5; -- 页码 3
-- 公式:startIndex = (currPage - 1) * pageSize
-- 计算一共有多少页
		-- 方法一:result = totalCount%pageSize,如果余数result为0,
						-- totalPage = totalCount / pageSize
						-- 如果余数result不为0,
            -- totalPage = totalCount / pageSize + 1;
		-- 方法二:totalPage = (totalCount + pageSize - 1) / pageSize
Pageing工具类
public class PaginationBean<T> { private List<T> dataList; private int currPage; private int totalPage; public List<T> getDataList() { return dataList; } public void setDataList(List<T> dataList) { this.dataList = dataList; } public int getCurrPage() { return currPage; } public void setCurrPage(int currPage) { this.currPage = currPage; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } }
Servlet
@WebServlet("/showUserList")
public class ShowUserListServlet extends HttpServlet implements Servlet {
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String operation = request.getParameter("operation");
        String currPageStr = request.getParameter("currPage");
        int currPage = 0;
        
        IUserService userSevice = new UserServiceImpl();
        int totalPage = userSevice.getTotalPage();
        
        if ("首页".equals(operation) || operation == null || currPageStr == null || currPageStr.length() == 0) {
            
            currPage  = 1;
        } else if ("上一页".equals(operation)) {
            
            currPage = Integer.parseInt(currPageStr) - 1;
            if (currPage <= 0) {
                currPage = 1;
            }
        } else if ("下一页".equals(operation)) {
            
            currPage = Integer.parseInt(currPageStr) + 1;
            if (currPage >= totalPage) {
                currPage = totalPage;
            }
        } else {
            
            currPage = totalPage;
        }
        
        List<TestUserBean> userList = userSevice.getUserListByCurrPage(currPage);
        
        PaginationBean<TestUserBean> pageBean = new PaginationBean<TestUserBean>();
        pageBean.setDataList(userList);
        pageBean.setCurrPage(currPage);
        pageBean.setTotalPage(totalPage);
        
        request.setAttribute("page", pageBean);
        
        request.getRequestDispatcher("/userList.jsp").forward(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%-- 引入JSTL --%> <%@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> <style> table { border-collapse: collapse; } </style> </head> <body> <table border="1"> <tr> <th>ID</th> <th>姓名</th> <th>密码</th> <th>身份证号</th> </tr> <c:forEach items="${page.dataList }" var="user"> <tr> <td>${user.id }</td> <td>${user.name }</td> <td>${user.pwd }</td> <td>${user.idCard }</td> </tr> </c:forEach> </table> <span>第${page.currPage }页/共${page.totalPage }页</span> <br> <br> <form action="showUserList" method="get"> <input type="submit" name="operation" value="首页"> <input type="submit" name="operation" value="上一页"> <input type="submit" name="operation" value="下一页"> <input type="submit" name="operation" value="尾页"> <input type="hidden" name="currPage" value="${page.currPage }"> </form> </body> </html>
标签:jsp ring std orm title start ext parameter hidden
原文地址:https://www.cnblogs.com/rookie97/p/12081485.html