欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

Java

當前位置:主頁 > 軟件編程 > Java >

JavaWeb分頁的實現(xiàn)代碼實例

來源:本站原創(chuàng)|時間:2020-01-10|欄目:Java|點擊: 次

這篇文章主要介紹了JavaWeb分頁的實現(xiàn)代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

分頁的分類

分頁的實現(xiàn)分為真分頁和假分頁兩種。

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

實現(xiàn)原理: SELECT * FROM xxx [WHERE...] LIMIT ?, 10;

第一個參數(shù)是開始數(shù)據(jù)的索引位置

10是要查詢多少條數(shù)據(jù),即每頁顯示的條數(shù)

優(yōu)點: 不會造成內(nèi)存溢出

缺點: 翻頁的速度比較慢

2.假分頁(邏輯分頁):

實現(xiàn)原理: 一次性將所有的數(shù)據(jù)查詢出來放在內(nèi)存之中,每次需要查詢的時候就直接從內(nèi)存之中去取出相應索引區(qū)間的數(shù)據(jù)

優(yōu)點: 分頁的速度比較快

缺點: 可能造成內(nèi)存溢出

分頁的一些術語:

  • -- 數(shù)據(jù)總條數(shù): totalCount : select count(1) from t_user;
  • -- 每頁顯示條數(shù):pageSize
  • -- 總頁數(shù):totalPage
  • -- 當前頁:currPage
  • -- 起始索引: startIndex

-- 通過當前頁碼查詢第幾頁的數(shù)據(jù)

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,如果余數(shù)result為0,

-- totalPage = totalCount / pageSize

-- 如果余數(shù)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>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持我們。

上一篇:簡單了解Spring中BeanFactory與FactoryBean的區(qū)別

欄    目:Java

下一篇:詳解IDEA JUnit5測試套件運行錯誤的問題

本文標題:JavaWeb分頁的實現(xiàn)代碼實例

本文地址:http://mengdiqiu.com.cn/a1/Java/8880.html

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權所有