Java分頁
首先說一下什麼是分頁:分頁就是將所有數據分開展示給用戶的技術,通俗點說就像大家的書一樣,我們可以通過頁數來找到自己想要的內容,Java分頁也是如此。
Java分頁的意義
分頁確實有效,但它一定會加大系統的複雜度,但可否不分頁呢?如果學習的話當然可以.,因爲數據庫的數據比較少;但是對於企業信息系統來說數據量不會限制在一個小範圍內,利用分頁可以高效的,找到數據,並且減輕對服務器的壓力。
如何實現分頁
首先我們需要一個工具類Page.Java來做輔助,通過封裝來限制每頁的數據量,總數據量,和每頁的頁碼,最後保存到list集合中,進行讀取。
public class Page {
//總頁數
private int totalPageCount = 0;
//每頁顯示記錄數
private int pageSize = 3;
//當前總數
private int totalCount;
//當前頁碼
private int currPageNo = 1;
//每頁的集合
private List<Student> studentList;
public int getTotalPageCount() {
return totalPageCount;
}
public void setTotalPageCount(int totalPageCount) {
this.totalPageCount = totalPageCount;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
if(pageSize>0)
this.pageSize = pageSize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
if(totalCount>0)
this.totalCount = totalCount;
//計算總頁數
totalPageCount=this.totalCount%pageSize==0?(this.totalCount/pageSize):(this.totalCount/pageSize+1);
}
public int getCurrPageNo() {
if(totalPageCount==0)
return 0;
return currPageNo;
}
public void setCurrPageNo(int currPageNo) {
if(currPageNo>0)
this.currPageNo = currPageNo;
}
public List<Student> getStudentList() {
return studentList;
}
public void setStudentList(List<Student> studentList) {
this.studentList = studentList;
}
}
我們在這裏需要自己搭三層
然後我們來到Servlet頁面
package cn.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import cn.dao.StudentDao;
import cn.dao.impl.StudentDaoImpl;
import cn.entity.Page;
import cn.entity.Student;
public class ShowServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
//從頁面接受頁碼
String strNum=request.getParameter("no");
int no=1;
if(strNum==null || strNum==""){
}else{
no=Integer.parseInt(strNum);
}
StudentDao dao = new StudentDaoImpl();
//總數量 從數據庫裏面查出來的
int totalCount = dao.getTotalCount();
Page p = new Page();
//當前頁面
p.setCurrPageNo(no);
//當前總數
p.setTotalCount(totalCount);
//查詢當前頁面的 limit ?,?
List<Student> list = dao.getPageList(p.getCurrPageNo(), p.getPageSize());
//賦給page裏面的List
p.setStudentList(list);
request.setAttribute("list", p.getStudentList());
request.setAttribute("count", p.getTotalPageCount());
//當前頁碼
request.setAttribute("no",p.getCurrPageNo());
request.getRequestDispatcher("show.jsp").forward(request, response);
/*out.print("新聞總數量:"+p.getTotalCount()+"<br/>");
out.print("每條數量是"+p.getPageSize()+"<br/>");
out.print("總頁數"+p.getTotalPageCount()+"<br/>");
out.print("當前是第"+p.getCurrPageNo()+"頁<br/>");
List<Student> list = dao.getPageList(p.getCurrPageNo(), p.getPageSize());
p.setStudentList(list);
String json = JSON.toJSONString(p.getStudentList());
out.print(json);*/
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
最後來到Show.jsp頁面
<%@ page language=“java” import=“java.util.*” pageEncoding=“utf-8”%>
<%@ taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c” %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"😕/"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<title>My JSP 'show.jsp' starting page</title>
<table>
<tr>
<td>編號</td>
<td>姓名</td>
<td>密碼</td>
</tr>
<c:forEach items="${list }" var="item">
<tr>
<td>${item.id }</td>
<td>${item.name }</td>
<td>${item.pwd }</td>
</tr>
</c:forEach>
<tr>
<td><a href="a?no=${no-1 }">上一頁</a></td>
<td><a href="a?no=${no+1 }">下一頁</a></td>
<td><a href="a?no=1 ">首頁</a></td>
<td><a href="a?no=${count} ">尾頁</a></td>
</tr>
</table>
關於數據庫,我們使用的是Mysql。表中有三個屬性:編號,姓名,密碼
然後加幾條測試數據,給大家發一下我的效果圖
Tomcat我用的是7.x,其他版本沒有試過,如果過程中有什麼不懂的可以加我微信號:lxl1052681394
或者掃下面二維碼