濮阳杆衣贸易有限公司

主頁 > 知識庫 > JSP實現(xiàn)分頁效果

JSP實現(xiàn)分頁效果

熱門標(biāo)簽:自動外呼系統(tǒng)怎么防止封卡 客服外呼系統(tǒng)呼叫中心 土地證宗地圖標(biāo)注符號 保定電銷機器人軟件 電話機器人案例 鎮(zhèn)江云外呼系統(tǒng)怎么樣 成都銷售外呼系統(tǒng)公司 電話機器人銷售公司嗎 vue 地圖標(biāo)注拖拽

本文實例為大家分享了JSP實現(xiàn)分頁的具體代碼,供大家參考,具體內(nèi)容如下

咱們在瀏覽網(wǎng)頁的時候,當(dāng)一個頁面的數(shù)據(jù)不足以展示完全所有的內(nèi)容,一般都涉及到分頁,下一頁的功能該怎么實現(xiàn)呢?首先我們來分析一下:

那么直接上代碼:

這里需要備注一下,本次的代碼是在對三層優(yōu)化之后進行操作的,所以我先把數(shù)據(jù)訪問層的重構(gòu)代碼貼出來:

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=falseserverTimezone=UTF-8useSSL=falseserverTimezone = GMT";
 public static final String username = "root";
 public static final String password = "zxy170518.";
 public static Connection connection = null;//鏈接數(shù)據(jù)庫
 public static PreparedStatement pstmt=null;//執(zhí)行sql語句 
 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;iobj.length;i++)
 {
 pstmt.setObject(i+1, obj[i]);//進行更新動作
 }
 }
 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ù)庫操作功能,很好懂,就不多解釋了;
對于數(shù)據(jù)訪問層的Dao:

public int getTotalCount()//查詢數(shù)據(jù)總數(shù)
 {
 String sql="select count(1) from student";
 return DButil.getTotalCount(sql);
 }
 
 public ListStudent> findStudentByPage(int currentPage,int pageSize)//currentPage:當(dāng)前頁數(shù);pageSize頁面所能容納的最大數(shù)據(jù)量
 {
 String sql="select * from student limit ? , ?";
 Object[] obj= {currentPage*pageSize,pageSize};
 ListStudent> 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;
 }

對于業(yè)務(wù)邏輯層:

Server:

public int getTotalCount()
 {
 return studentdao.getTotalCount();
 }
 
 public ListStudent> findStudentByPage(int currentPage,int pageSize)
 {
 return studentdao.findStudentByPage(currentPage, pageSize);
 }

對于視圖層的后臺代碼:

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)//判斷是否為第一次進行訪問
 {
 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)//默認一頁3個內(nèi)容
 {
 tmp2="3";
 }
 
 int pageSize=Integer.parseInt(tmp2);
 pag.setPageSize(pageSize);
 ListStudent> 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);
 }

}

還有一個實體類:Page:

package org.ThreeLayer.Entity;

import java.util.List;

public class Page_S {//為了不出現(xiàn)于重名,改了一下
 private int currentPage;
 private int pageSize;//頁面大小,即頁面數(shù)據(jù)個數(shù)
 private int totalCount;//總數(shù)據(jù)
 private int totalPage;//總頁數(shù)
 private ListStudent> students;
 public Page_S() {
 }
 public Page_S(int currentPage, int pageSize, int totalCount, int totalPage, ListStudent> 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 ListStudent> getStudents() {
 return students;
 }
 public void setStudents(ListStudent> 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é)號/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)//用戶位于首頁的時候
 {
 %> 
 a href="findStudentByPage?currentPage=%=pagg.getCurrentPage()+1%>" >下一頁/a>
 a href="findStudentByPage?currentPage=%=pagg.getTotalPage()-1%>" >尾頁/a>
 % 
 }else if(pagg.getCurrentPage()==pagg.getTotalPage()-1)//用戶位于尾頁的時候
 {
 %>
 a href="findStudentByPage?currentPage=0" >首頁/a>
 a href="findStudentByPage?currentPage=%=pagg.getCurrentPage()-1%>" >上一頁/a>
 % 
 }else//用戶位于中間頁面的時候
 {
 %> a href="findStudentByPage?currentPage=0" >首頁/a>
 a href="findStudentByPage?currentPage=%=pagg.getCurrentPage()+1%>" >下一頁/a>
 a href="findStudentByPage?currentPage=%=pagg.getCurrentPage()-1%>" >上一頁/a>
 a href="findStudentByPage?currentPage=%=pagg.getTotalPage()-1%>" >尾頁/a>
 %
 }
 %>
 
 br>
/body>
/html>

看一下效果圖:

首先看數(shù)據(jù)庫內(nèi)容:

然后是首頁:

下一頁:

最后是尾頁:

總的說明一下:

首先對于功能的闡述,第一步計算總的數(shù)據(jù)量,然后規(guī)定默認容量大小為3,最終在jsp代碼中加上跟用戶進行交互的功能,即讓用戶選擇一頁多少內(nèi)容(由于我寫的那個有點bug,就先不貼,等后面自己能完美實現(xiàn)之后,再更新),之后對前端數(shù)據(jù)進行打包,要思考的是,我們對于這個功能我們所需要的數(shù)據(jù)有哪些呢?首先,總數(shù)據(jù)量要吧?然后要存放總的數(shù)據(jù)內(nèi)容吧?然后頁面大小需要吧?然后用戶所在頁面的那個頁面位置的數(shù)要吧?最后一個就是通過總數(shù)據(jù)量和頁面大小計算出來的總頁面數(shù)也需要吧?所以,一共就需要記錄5個屬性值,那就打包成一個JavaBean吧,前面代碼也貼出來了。最后要提一點,對于如果我第一次進行訪問頁面的時候,我應(yīng)該是有一些屬性值是為null的,這樣是會報空指針異常的,那么就要進行一些小小的處理,哪些呢?比如如果用戶第一次進行訪問,系統(tǒng)是收不到用戶當(dāng)前所在頁面的頁面數(shù)值的,那么就要判斷一下,(此處上代碼)如果是第一次進行訪問,那么就給與一個默認值0,也就是第一頁,那么就處理好了這個小問題了,諸如此類問題還有就是用戶在進行選擇一頁多少內(nèi)容的時候,也是需要進行賦予一個默認值的,不然也會報空指針。然后對于web.xml文件內(nèi)容的設(shè)置,首頁應(yīng)該設(shè)置為實現(xiàn)分頁功能的Servlet,因為你每做一次翻頁或者首次訪問,雖然都是在index.jsp中,但是你需要把每次做完動作之后得到的新的內(nèi)容進行請求轉(zhuǎn)發(fā),這樣才能實現(xiàn)更新,不然程序會報錯。

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

您可能感興趣的文章:
  • js簡單的分頁器插件代碼實例
  • jquery.pager.js實現(xiàn)分頁效果
  • laypage.js分頁插件使用方法詳解
  • vue+vuex+json-seiver實現(xiàn)數(shù)據(jù)展示+分頁功能
  • NodeJs操作MongoDB教程之分頁功能以及常見問題
  • 基于vue.js實現(xiàn)分頁查詢功能
  • JS實現(xiàn)的簡單分頁功能示例
  • JS實現(xiàn)前端動態(tài)分頁碼代碼實例

標(biāo)簽:懷化 成都 內(nèi)江 麗江 重慶 天津 公主嶺 臺灣

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《JSP實現(xiàn)分頁效果》,本文關(guān)鍵詞  JSP,實現(xiàn),分頁,效果,JSP,實現(xiàn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《JSP實現(xiàn)分頁效果》相關(guān)的同類信息!
  • 本頁收集關(guān)于JSP實現(xiàn)分頁效果的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    黎城县| 卫辉市| 武冈市| 湛江市| 丰镇市| 志丹县| 高要市| 通榆县| 确山县| 涟水县| 文登市| 东台市| 遂宁市| 吴江市| 平原县| 酒泉市| 玛沁县| 灵寿县| 湖北省| 旬邑县| 旺苍县| 鄂尔多斯市| 酒泉市| 马尔康县| 五峰| 得荣县| 休宁县| 斗六市| 吴桥县| 汝州市| 临夏县| 瓮安县| 宁都县| 鄢陵县| 阿鲁科尔沁旗| 九江市| 武汉市| 南岸区| 吉首市| 含山县| 平利县|