JSP實(shí)現(xiàn)分頁(yè)效果
本文實(shí)例為大家分享了JSP實(shí)現(xiàn)分頁(yè)的具體代碼,供大家參考,具體內(nèi)容如下
咱們?cè)跒g覽網(wǎng)頁(yè)的時(shí)候,當(dāng)一個(gè)頁(yè)面的數(shù)據(jù)不足以展示完全所有的內(nèi)容,一般都涉及到分頁(yè),下一頁(yè)的功能該怎么實(shí)現(xiàn)呢?首先我們來(lái)分析一下:

那么直接上代碼:
這里需要備注一下,本次的代碼是在對(duì)三層優(yōu)化之后進(jìn)行操作的,所以我先把數(shù)據(jù)訪問(wèn)層的重構(gòu)代碼貼出來(lái):
package org.ThreeLayer.DButil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.ThreeLayer.Entity.Student;
public class DButil
{
public static final String driver = "com.mysql.cj.jdbc.Driver";
public static final String url = "jdbc:mysql://localhost:3306/zxy?&useSSL=false&serverTimezone=UTF-8&useSSL=false&serverTimezone = GMT";
public static final String username = "root";
public static final String password = "zxy170518.";
public static Connection connection = null;//鏈接數(shù)據(jù)庫(kù)
public static PreparedStatement pstmt=null;//執(zhí)行sql語(yǔ)句
public static ResultSet rs=null;
public static Connection getConnection() throws SQLException, ClassNotFoundException
{
Class.forName(driver);
return DriverManager.getConnection(url,username,password);
}
public static int getTotalCount(String sql)
{
int count=0;
try
{
pstmt=createPrepareStatement(sql, null);
rs=pstmt.executeQuery();
if(rs.next())
{
count=rs.getInt(1);
}
}catch(SQLException e)
{
e.printStackTrace();
}catch(ClassNotFoundException e)
{
e.printStackTrace();
}catch(Exception e)
{
e.printStackTrace();
}finally
{
closeAll(connection, pstmt, rs);
}
return count;
}
public static PreparedStatement createPrepareStatement(String sql,Object[] obj) throws ClassNotFoundException, SQLException
{
pstmt=getConnection().prepareStatement(sql);
if(obj!=null)
{
for(int i=0;i<obj.length;i++)
{
pstmt.setObject(i+1, obj[i]);//進(jìn)行更新動(dòng)作
}
}
return pstmt;
}
public static boolean UpdateSQL(String sql,Object[] obj)
{
try
{
pstmt=createPrepareStatement(sql, obj);
int count=pstmt.executeUpdate();
if(count>0)
{
return true;
}
else
{
return false;
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}finally
{
closeAll(connection,pstmt,rs);
}
}
public static ResultSet FindSQL(String sql,Object[] obj)
{
try
{
pstmt=createPrepareStatement(sql, obj);
rs=pstmt.executeQuery();
return rs;
}catch(ClassNotFoundException e)
{
e.printStackTrace();
return rs;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return rs;
}catch(Exception e)
{
e.printStackTrace();
return rs;
}
}
public static void closeAll(Connection connection,PreparedStatement pstmt,ResultSet rs)
{
try
{
if(connection!=null);
connection.close();
if(pstmt!=null);
pstmt.close();
if(rs!=null);
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
基本上就是普通的數(shù)據(jù)庫(kù)操作功能,很好懂,就不多解釋了;
對(duì)于數(shù)據(jù)訪問(wèn)層的Dao:
public int getTotalCount()//查詢(xún)數(shù)據(jù)總數(shù)
{
String sql="select count(1) from student";
return DButil.getTotalCount(sql);
}
public List<Student> findStudentByPage(int currentPage,int pageSize)//currentPage:當(dāng)前頁(yè)數(shù);pageSize頁(yè)面所能容納的最大數(shù)據(jù)量
{
String sql="select * from student limit ? , ?";
Object[] obj= {currentPage*pageSize,pageSize};
List<Student> students=new ArrayList<>();
ResultSet rs=DButil.FindSQL(sql, obj);
try {
while(rs.next())
{
Student student=new Student(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4));
students.add(student);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return students;
}
對(duì)于業(yè)務(wù)邏輯層:
Server:
public int getTotalCount()
{
return studentdao.getTotalCount();
}
public List<Student> findStudentByPage(int currentPage,int pageSize)
{
return studentdao.findStudentByPage(currentPage, pageSize);
}
對(duì)于視圖層的后臺(tái)代碼:
Servlet:
package org.Three.Servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.ThreeLayer.Entity.Page_S;
import org.ThreeLayer.Entity.Student;
import org.ThreeLayer.Server.Student_Server;
public class findStudentByPage extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Student_Server studentS=new Student_Server();
// int currentPage=2;
Page_S pag=new Page_S();
String tmp=request.getParameter("currentPage");
if(tmp==null)//判斷是否為第一次進(jìn)行訪問(wèn)
{
tmp="0";
}
int sum=studentS.getTotalCount();
pag.setTotalCount(sum);
int currentPage= Integer.parseInt(tmp);
pag.setCurrentPage(currentPage);
String tmp2=request.getParameter("choose");
if(tmp2==null)//默認(rèn)一頁(yè)3個(gè)內(nèi)容
{
tmp2="3";
}
int pageSize=Integer.parseInt(tmp2);
pag.setPageSize(pageSize);
List<Student> students =studentS.findStudentByPage(currentPage, pageSize);
pag.setStudents(students);
request.setAttribute("pag", pag);
request.getRequestDispatcher("index.jsp").forward(request, response);
System.out.print(students);
System.out.print(sum);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
還有一個(gè)實(shí)體類(lèi):Page:
package org.ThreeLayer.Entity;
import java.util.List;
public class Page_S {//為了不出現(xiàn)于重名,改了一下
private int currentPage;
private int pageSize;//頁(yè)面大小,即頁(yè)面數(shù)據(jù)個(gè)數(shù)
private int totalCount;//總數(shù)據(jù)
private int totalPage;//總頁(yè)數(shù)
private List<Student> students;
public Page_S() {
}
public Page_S(int currentPage, int pageSize, int totalCount, int totalPage, List<Student> students) {
this.currentPage = currentPage;
this.pageSize = pageSize;
this.totalCount = totalCount;
this.totalPage = totalPage;
this.students = students;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
this.totalPage=this.totalCount%this.pageSize==0?this.totalCount/this.pageSize:this.totalCount/this.pageSize+1;
}
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<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
}
最后貼上index.jsp:
<%@page import="java.util.List"%>
<%@page import="org.ThreeLayer.Entity.Student"%>
<%@page import="org.ThreeLayer.Entity.Page_S"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>學(xué)生信息管理</title>
</head>
<body>
<table border=1px>
<tr>
<th>學(xué)號(hào)</th>
<th>姓名</th>
<th>性別</th>
<th>操作</th>
</tr>
<%
Page_S pagg=(Page_S)request.getAttribute("pag");
for(Student student:pagg.getStudents())
{
%>
<tr>
<th><a href="FindStudentById_Servlet?uid=<%=student.getId()%>" ><%=student.getId() %></a></th>
<th><%=student.getName() %></th>
<th><%=student.getSex() %></th>
<th><a href="DeleteStudent_Servlet?uid=<%=student.getId()%>" >刪除</a></th>
</tr>
<%
}
%>
</table>
<a href="add.jsp" >增加</a>
<%
if(pagg.getCurrentPage()==0)//用戶(hù)位于首頁(yè)的時(shí)候
{
%>
<a href="findStudentByPage?currentPage=<%=pagg.getCurrentPage()+1%>" >下一頁(yè)</a>
<a href="findStudentByPage?currentPage=<%=pagg.getTotalPage()-1%>" >尾頁(yè)</a>
<%
}else if(pagg.getCurrentPage()==pagg.getTotalPage()-1)//用戶(hù)位于尾頁(yè)的時(shí)候
{
%>
<a href="findStudentByPage?currentPage=0" >首頁(yè)</a>
<a href="findStudentByPage?currentPage=<%=pagg.getCurrentPage()-1%>" >上一頁(yè)</a>
<%
}else//用戶(hù)位于中間頁(yè)面的時(shí)候
{
%> <a href="findStudentByPage?currentPage=0" >首頁(yè)</a>
<a href="findStudentByPage?currentPage=<%=pagg.getCurrentPage()+1%>" >下一頁(yè)</a>
<a href="findStudentByPage?currentPage=<%=pagg.getCurrentPage()-1%>" >上一頁(yè)</a>
<a href="findStudentByPage?currentPage=<%=pagg.getTotalPage()-1%>" >尾頁(yè)</a>
<%
}
%>
<br>
</body>
</html>
看一下效果圖:
首先看數(shù)據(jù)庫(kù)內(nèi)容:

然后是首頁(yè):

下一頁(yè):

最后是尾頁(yè):

總的說(shuō)明一下:
首先對(duì)于功能的闡述,第一步計(jì)算總的數(shù)據(jù)量,然后規(guī)定默認(rèn)容量大小為3,最終在jsp代碼中加上跟用戶(hù)進(jìn)行交互的功能,即讓用戶(hù)選擇一頁(yè)多少內(nèi)容(由于我寫(xiě)的那個(gè)有點(diǎn)bug,就先不貼,等后面自己能完美實(shí)現(xiàn)之后,再更新),之后對(duì)前端數(shù)據(jù)進(jìn)行打包,要思考的是,我們對(duì)于這個(gè)功能我們所需要的數(shù)據(jù)有哪些呢?首先,總數(shù)據(jù)量要吧?然后要存放總的數(shù)據(jù)內(nèi)容吧?然后頁(yè)面大小需要吧?然后用戶(hù)所在頁(yè)面的那個(gè)頁(yè)面位置的數(shù)要吧?最后一個(gè)就是通過(guò)總數(shù)據(jù)量和頁(yè)面大小計(jì)算出來(lái)的總頁(yè)面數(shù)也需要吧?所以,一共就需要記錄5個(gè)屬性值,那就打包成一個(gè)JavaBean吧,前面代碼也貼出來(lái)了。最后要提一點(diǎn),對(duì)于如果我第一次進(jìn)行訪問(wèn)頁(yè)面的時(shí)候,我應(yīng)該是有一些屬性值是為null的,這樣是會(huì)報(bào)空指針異常的,那么就要進(jìn)行一些小小的處理,哪些呢?比如如果用戶(hù)第一次進(jìn)行訪問(wèn),系統(tǒng)是收不到用戶(hù)當(dāng)前所在頁(yè)面的頁(yè)面數(shù)值的,那么就要判斷一下,(此處上代碼)如果是第一次進(jìn)行訪問(wèn),那么就給與一個(gè)默認(rèn)值0,也就是第一頁(yè),那么就處理好了這個(gè)小問(wèn)題了,諸如此類(lèi)問(wèn)題還有就是用戶(hù)在進(jìn)行選擇一頁(yè)多少內(nèi)容的時(shí)候,也是需要進(jìn)行賦予一個(gè)默認(rèn)值的,不然也會(huì)報(bào)空指針。然后對(duì)于web.xml文件內(nèi)容的設(shè)置,首頁(yè)應(yīng)該設(shè)置為實(shí)現(xiàn)分頁(yè)功能的Servlet,因?yàn)槟忝孔鲆淮畏?yè)或者首次訪問(wèn),雖然都是在index.jsp中,但是你需要把每次做完動(dòng)作之后得到的新的內(nèi)容進(jìn)行請(qǐng)求轉(zhuǎn)發(fā),這樣才能實(shí)現(xiàn)更新,不然程序會(huì)報(bào)錯(cuò)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- js簡(jiǎn)單的分頁(yè)器插件代碼實(shí)例
- jquery.pager.js實(shí)現(xiàn)分頁(yè)效果
- laypage.js分頁(yè)插件使用方法詳解
- vue+vuex+json-seiver實(shí)現(xiàn)數(shù)據(jù)展示+分頁(yè)功能
- NodeJs操作MongoDB教程之分頁(yè)功能以及常見(jiàn)問(wèn)題
- 基于vue.js實(shí)現(xiàn)分頁(yè)查詢(xún)功能
- JS實(shí)現(xiàn)的簡(jiǎn)單分頁(yè)功能示例
- JS實(shí)現(xiàn)前端動(dòng)態(tài)分頁(yè)碼代碼實(shí)例
相關(guān)文章
JSP教程之使用JavaBean完成業(yè)務(wù)邏輯的方法
這篇文章主要介紹了JSP教程之使用JavaBean完成業(yè)務(wù)邏輯的方法,較為詳細(xì)的分析了JavaBean完成業(yè)務(wù)邏輯所涉及的相關(guān)概念及使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
jsp中實(shí)現(xiàn)帶滾動(dòng)條的table表格實(shí)例代碼
下面小編就為大家?guī)?lái)一篇jsp中實(shí)現(xiàn)帶滾動(dòng)條的table表格實(shí)例代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10
Jsp中解決session過(guò)期跳轉(zhuǎn)到登陸頁(yè)面并跳出iframe框架的方法
這里我們是介紹一個(gè)網(wǎng)站管理后臺(tái)三個(gè)框架頁(yè)面當(dāng)我們的jsp定義的session變量超時(shí)時(shí)用戶(hù)點(diǎn)擊時(shí)自動(dòng)退出框架頁(yè)面并跳到登錄頁(yè)面去了,下面我來(lái)給大家演示一個(gè)實(shí)例2013-08-08
jsp防止跨域提交數(shù)據(jù)的具體實(shí)現(xiàn)
這篇文章主要介紹了jsp防止跨域提交數(shù)據(jù)的具體實(shí)現(xiàn),需要的朋友可以參考下2014-02-02
解決request.getParameter取值后的if判斷為NULL的問(wèn)題
這篇文章主要介紹了解決request.getParameter取值后的if判斷為NULL的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

