分頁探究--Filter+JSTL

  最近卡了一個功能就是分頁,查了很多資料,分頁大概是兩種類型:一種是把數據庫的東西全部查出來然后放在session里,用list一頁一頁傳到頁面,這樣的消耗比較大;另一種就是使用sql語句的limit來進行數據庫分頁查詢。我使用的是后者

  大致邏輯: (1)需要currentPage,count屬性。

      ? (2)需要注意current不能點擊。

        (3)全使用a標簽進行頁面跳轉。并附上請求頁碼。

       ?(4)初始化查詢0頁,并用filter裝入list中,在頁面顯示的時候方便遍歷。

        (5)過程:頁面加載->filter查詢初始數據裝入request->頁面遍歷并計算出頁碼請求附帶在url后->請求發出后filter使用getParameter獲得頁碼對數據庫進行查詢,并裝入list中->頁面加載的時候遍歷list出現新數據。

  頁面如下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page contentType="text/html; charset=utf-8" %><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>分頁列表</title>
</head>
<body>
<center><%int currenPage=((Integer)request.getAttribute("currenPage")).intValue();int count=((Integer)request.getAttribute("count")).intValue();%><table border="1px"><tr><td>ID</td><td>用戶名</td><td>性別</td><td>年齡</td></tr><c:forEach var="usr" items="${list}"><tr><td>${usr.id}</td><td>${usr.name}</td><td>${usr.sex}</td><td>${usr.age}</td></tr></c:forEach></table><%int prePage=currenPage-1;if(currenPage==1)prePage=currenPage;%><a href="Demo2.jsp?<%="curren="+prePage%>">上一頁</a> <%int i=1;int end=currenPage+5;if(currenPage>5){i=currenPage-5;}if(end>count/10){end=count/10;System.out.println("end="+end);}for(;i<=end;i++){System.out.println("i="+i);if(i == (currenPage)){%>[<%=currenPage%>] <% }else{%><a href="Demo2.jsp?<%="curren="+i%>"><%=i%></a> <% }}%><%int nextPage=currenPage+1;if(nextPage>count/10)nextPage--;%><a href="Demo2.jsp?<%="curren="+nextPage%>">下一頁</a> </center>
</body>
</html>

  Filter如下

package filter;import java.io.IOException;
import java.util.List;import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;import dividedpage.SelectService;
import model.test_u;/*** Servlet Filter implementation class divideFilter*/
@WebFilter("/Demo2.jsp")
public class divideFilter implements Filter {private static final long serialVersionUID = 1L;private int start=0;private int size=10;private SelectService ss;private List<test_u> list;/*** Default constructor. */public divideFilter() {// TODO Auto-generated constructor stubss = new SelectService();}/*** @see Filter#destroy()*/public void destroy() {// TODO Auto-generated method stub
    }/*** @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)*/public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {// TODO Auto-generated method stub// place your code hereString cu=request.getParameter("curren");if(cu!=null){start=Integer.parseInt(cu);}System.out.println(start);list = ss.selectLimit((start-1)*size, size);int count = ss.getConut(); request.setAttribute("list", list);request.setAttribute("count", count);request.setAttribute("currenPage", start);// pass the request along the filter chainSystem.out.println("執行過濾");chain.doFilter(request, response);}/*** @see Filter#init(FilterConfig)*/public void init(FilterConfig fConfig) throws ServletException {// TODO Auto-generated method stub
    }}

  JDBC如下

package DAO;
import java.sql.*;
public class Connect2DB {String  driver="com.mysql.jdbc.Driver";  Connection con;  String url="jdbc:mysql://localhost:3306/MyData";  String user="root";  String pwd="qwert123";public Connect2DB(){connection2MYSQL() ;}public void connection2MYSQL()  {  try {  Class.forName(driver);  con=DriverManager.getConnection(url,user,pwd);  if(!con.isClosed())  System.out.println("連接成功");  } catch (Exception e) {  e.printStackTrace();  }                  }public Connection getConn(){return con;}
}
package DAO;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import model.test_u;public class OperatorDB {private Connection con;public OperatorDB(){con=new Connect2DB().getConn();}public void addUser(test_u u){String sql="insert into test_u(id,name,sex,age) values(?,?,?,?)";PreparedStatement ps; try {ps=con.prepareStatement(sql);ps.setInt(1, u.getId());ps.setString(2, u.getName());ps.setString(3, u.getSex());ps.setString(4, u.getAge());ps.executeUpdate();} catch (SQLException e) {// TODO Auto-generated catch block
            e.printStackTrace();}}public void delUserById(int id){String sql="delete from test_u where stu_id = ?";PreparedStatement ps; try {ps=con.prepareStatement(sql);ps.setInt(1, id);ps.executeUpdate();} catch (SQLException e) {// TODO Auto-generated catch block
            e.printStackTrace();}}public List<test_u> selectLimit(int start,int size){String sql = "select * from test_u limit ?,?";List<test_u> result=new ArrayList<test_u>();PreparedStatement ps;try {ps = con.prepareStatement(sql);ps.setInt(1, start);ps.setInt(2, size);ResultSet rs = ps.executeQuery();while(rs.next()){int id=rs.getInt("id");String name=rs.getString("name");String sex=rs.getString("sex");String age=rs.getString("age");test_u t=new test_u(id,name,sex,age);result.add(t);}} catch (SQLException e) {// TODO Auto-generated catch block
            e.printStackTrace();}return result;}public int getCount(){String sql="SELECT COUNT(*) FROM test_u";int rowCount = 0;try {PreparedStatement ps;ps = con.prepareStatement(sql);ResultSet rs = ps.executeQuery(sql);rs.next(); rowCount = rs.getInt(1);} catch (SQLException e) {// TODO Auto-generated catch block
            e.printStackTrace();}      return rowCount;}public void close(){try {if(!con.isClosed())con.close();} catch (SQLException e) {// TODO Auto-generated catch block
            e.printStackTrace();}}
}

  Service如下

package dividedpage;import java.util.List;import DAO.OperatorDB;
import model.test_u;public class SelectService {public List<test_u> selectLimit(int start,int size){OperatorDB odb=new OperatorDB();List<test_u> list=odb.selectLimit(start, size);odb.close();return list;}public int getConut(){OperatorDB odb=new OperatorDB();int count = odb.getCount();odb.close();return count;}
}

  Bean如下

package model;public class test_u {private int id;private String name;private String sex;private String age;public test_u(){}public test_u(int id, String name, String sex, String age) {super();this.id = id;this.name = name;this.sex = sex;this.age = age;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}}

  數據表如下,插入100條記錄

  感覺代碼很冗余,頁面不夠干凈,不過也訓練了分頁的思想。

  下列標簽欄全是a標簽,上一頁current-1,下一頁current+1;需要注意頁面邊界(最大,最小頁)。查詢limit大概是((current-1)*size,size)這樣的公式。

  目錄樹如下:

  JSTL需要下載個jar包,很容易找到,添加他們進path就好。

轉載于:https://www.cnblogs.com/chentingk/p/5825957.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/260744.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/260744.shtml
英文地址,請注明出處:http://en.pswp.cn/news/260744.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

iPhone開發資料之內存管理 ,循環引用導致的內存問題

iPhone開發資料之內存管理 ,循環引用導致的內存問題 https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447 http://en.wikipedia.org/wiki/Reference_counting 【IT168 技術文檔】開…

python能做大型游戲嗎_python有做大型游戲的潛力嗎?

著作權歸作者所有。商業轉載請聯系作者獲得授權&#xff0c;非商業轉載請注明出處。 豈止是有潛力&#xff0c;簡直是很合適&#xff01; 豬廠兩大游戲客戶端引擎&#xff0c;NeoX 和 Messiah&#xff0c;都使用 Python 作為腳本語言。 你最近所了解的比較火的掛著豬廠旗號的&a…

第4章 Python 數字圖像處理(DIP) - 頻率域濾波2 - 復數、傅里葉級數、連續單變量函數的傅里葉變換、卷積

目錄基本概念復數傅里葉級數沖激函數及其取樣&#xff08;篩選&#xff09;性質連續單變量函數的傅里葉變換卷積基本概念 復數 復數CCC的定義為 CRjI(4.3)C R jI \tag{4.3}CRjI(4.3) R,IR,IR,I為實數&#xff0c;RRR是實部&#xff0c;III是虛部&#xff0c;j?1j \sqrt{-…

不要迷失在技術的海洋中【轉】

轉自http://www.cnblogs.com/lovecherry/archive/2007/10/28/940555.html 不要迷失在技術的海洋中 技術就好像一片汪洋大海&#xff0c;越深入越望不到邊際。就拿自己的體驗來說吧&#xff0c;2000年的時候在學校搞ASP&#xff0c;覺得網頁開發就是這么簡單&#xff0c;把數據庫…

使用代碼設置Item級的權限(權限總結1)

itle in english:set Item Level Permission for SharePoint (MOSS/WSS) List/Document Library Programmatically 有些時候&#xff0c;我們需要為文檔庫里面某個文件設置特殊的權限&#xff0c;這個權限不繼承自列表權限&#xff0c;當然最簡單的最好是再創建一個列表&#…

echarts 4.0.4怎么下載_怎么讓ECharts的提示框tooltip自動輪播?

1. 怎么讓ECharts的提示框tooltip自動輪播?在用ECharts做大屏或者可視化展示項目的時候&#xff0c;讓提示框tooltip自動輪播是比較常見的需求&#xff0c;給大家推薦一個插件叫echarts-tooltip-auto-show,名字是有點長&#xff0c;但是挺好用的。在hover顯示tooltip之后&…

[React Native]高度自增長的TextInput組件

之前我們學習了從零學React Native之11 TextInput了解了TextInput相關的屬性。 在開發中,我們有時候有這樣的需求, 希望輸入區域的高度隨著輸入內容的長度而增長, 如下&#xff1a; 這時候我們需要自定義一個組件&#xff1a; 在項目中創建AutoExpandingTextInput.js import …

網站開啟Gzip壓縮-apache

找到并打開apache/conf目錄中的httpd.conf文件 httpd.conf中打開deflate_Module和headers_Module模塊&#xff0c;具體做法為將 如下兩句前面的#去掉&#xff1a;LoadModule deflate_module modules/mod_deflate.so LoadModule headers_module modules/mod_headers.so 3.配置文…

第4章 Python 數字圖像處理(DIP) - 頻率域濾波3 - 取樣和取樣函數的傅里葉變換、混疊

目錄取樣和取樣函數的傅里葉變換取樣取樣后的函數的傅里葉變換取樣定理混疊由取樣后的數據重建&#xff08;復原&#xff09;函數取樣和取樣函數的傅里葉變換 取樣 fˉ(t)f(t)sΔT(t)∑n?∞∞f(t)δ(t?nΔT)(4.27)\bar f(t) f(t)s_{\Delta T}(t) \sum_{n-\infty}^{\infty}…

[轉]Android開發,實現可多選的圖片ListView,便于批量操作

本文轉自&#xff1a;http://www.cnblogs.com/gergulo/archive/2011/06/14/2080629.html 之前項目需要實現一個可多選的圖片列表&#xff0c;用戶選中一到多張圖片后&#xff0c;批量上傳。但是網上有可多選普通列表的代碼、也有單純圖片列表的代碼&#xff0c;卻沒有兩者合并的…

個人信息安全影響評估指南_發布 | 網絡安全標準實踐指南—移動互聯網應用程序(App)收集使用個人信息自評估指南...

關于發布《網絡安全標準實踐指南—移動互聯網應用程序(App)收集使用個人信息自評估指南》的通知信安秘字[2020] 40號各有關單位&#xff1a;為落實《網絡安全法》相關要求&#xff0c;圍繞中央網信辦、工信部、公安部、市場監管總局聯合制定的《App違法違規收集使用個人信息行為…

Go的50度灰:Golang新開發者要注意的陷阱和常見錯誤

Go的50度灰&#xff1a;Golang新開發者要注意的陷阱和常見錯誤 http://colobu.com/2015/09/07/gotchas-and-common-mistakes-in-go-golang/

android intent和intent action大全

不管是頁面牽轉&#xff0c;還是傳遞數據&#xff0c;或是調用外部程序&#xff0c;系統功能都要用到intent。 在做了一些intent的例子之后&#xff0c;整理了一下intent&#xff0c;希望對大家有用。 由于intent內容太多&#xff0c;不可能真的寫全&#xff0c;難免會有遺落&a…

第4章 Python 數字圖像處理(DIP) - 頻率域濾波4 - 單變量的離散傅里葉變換DFT

目錄標題單變量的離散傅里葉變換由取樣后的函數的連續變換得到DFT取樣和頻率間隔的關系單變量的離散傅里葉變換 由取樣后的函數的連續變換得到DFT 對原函數的變換取樣后的業的發展的變換F~(μ)\tilde F(\mu)F~(μ)&#xff0c;但未給出取樣后的函數f~(t)\tilde f(t)f~?(t)的…

在線生成 CSS3 的工具

1) CSS Creator – Layout Design 2) CSS Menu Maker 3) CSS3 Please 4) CSS3 Generator 5) CSS Border Radius 6) CSS3 Gradient Generator 7) CSS3 Button Generator 8 ) Mike Plate’s CSS3 Playground 9) Border Image Generator 10) CSS3 WRAP 11) Button Maker 12) Font…

python image 轉成字節_就是這么牛!三行Python代碼,讓數據處理速度提高2到6倍

本文可以教你僅使用 3 行代碼&#xff0c;大大加快數據預處理的速度。Python 是機器學習領域內的首選編程語言&#xff0c;它易于使用&#xff0c;也有很多出色的庫來幫助你更快處理數據。但當我們面臨大量數據時&#xff0c;一些問題就會顯現……在默認情況下&#xff0c;Pyth…

sessionStorage 、localStorage 和 cookie 之間的區別(轉)

essionStorage 、localStorage 和 cookie 之間的區別(轉) 2012-05-08 14:29:19| 分類&#xff1a; HTML5CSS3WEBAPP|舉報|字號 訂閱 sessionStorage 和 localStorage 是HTML5 Web Storage API 提供的&#xff0c;可以方便的在web請求之間保存數據。有了本地數據&#xff0c;…

刪除文件夾里的圖片,打印刪除日志

1 #region 僵尸文件夾中的文件如果不在活文件列表中&#xff0c;刪之2 List<string> deadfile new List<string>();3 foreach(string str in lstZombileDic)4 {5 if(Direct…

第4章 Python 數字圖像處理(DIP) - 頻率域濾波5 - 二變量函數的傅里葉變換、圖像中的混疊、二維離散傅里葉變換及其反變換

目錄二變量函數的傅里葉變換二維沖激及其取樣性質二維連續傅里葉變換對二維取樣和二維取樣定理圖像中的混疊二維離散傅里葉變換及其反變換二變量函數的傅里葉變換 二維沖激及其取樣性質 兩個連續變量的沖激函數定義為&#xff1a; δ(t,z){1,tz00,others(4.52)\delta(t, z) …

巧用VC工程下的rc文件

巧用VC工程下的rc文件(發表時間: 2008-12-30 17:20:00) 【評論】 【打印】 【字體&#xff1a;大 中 小】 本文鏈接&#xff1a;http://blog.pfan.cn/miaowei/40117.html 復制鏈接 分享到&#xff1a; 0標簽:VC rc文件 資源文件 窗口尺寸設置 添加資源 縱觀真個的VC工程&a…