亲宝软件园·资讯

展开

JavaWeb分页的实现代码实例

人气:0

这篇文章主要介绍了JavaWeb分页的实现代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

分页的分类

分页的实现分为真分页和假分页两种。

1.真分页(物理分页):

实现原理: SELECT * FROM xxx [WHERE...] LIMIT ?, 10;

第一个参数是开始数据的索引位置

10是要查询多少条数据,即每页显示的条数

优点: 不会造成内存溢出

缺点: 翻页的速度比较慢

2.假分页(逻辑分页):

实现原理: 一次性将所有的数据查询出来放在内存之中,每次需要查询的时候就直接从内存之中去取出相应索引区间的数据

优点: 分页的速度比较快

缺点: 可能造成内存溢出

分页的一些术语:

-- 通过当前页码查询第几页的数据

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>SFZ号</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>
您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论