jsp的分頁技術,jsp分頁技術

來源:互聯網
上載者:User

jsp的分頁技術,jsp分頁技術

在jsp開發中,很多時候要用到分頁技術,另外純粹的分頁實現就佔了很多篇幅,所以使用javabean就十分有必要。

此文章依據實際代碼,想大家展示jsp中利用javabean實現分頁

目錄結構如下:


javabean的代碼如下:

package lytjb;
import java.sql.ResultSet;
import javax.servlet.http.HttpServletRequest;
public class fenye {
ResultSet CountTopicrs = null; // 初始化總記錄數Rs變數
ResultSet Pagirs = null; // 初始化分頁時Rs變數
public int intCountTopic = 0; // 主題總數
public int intPageSize;// 每頁顯示主題數
public int intPageCount;// 總頁數
public int intPage = 1; // 當前頁數
public String nowPage; // int i;
public String HttpFile;// 初始化當前頁intPage變數,以準確便擷取當前頁。 //當前的地址欄的檔案
DB db; // 定義Linkdb類的一個對象。


public fenye() {
intPageSize = 40; // 每頁顯示的記錄數目
db = new DB();
}


// Countsql:總記錄的Query字串。[形式為select count(*) from tablename]
// Pagisql :要分頁的Query字串。[形式為select * from tablename where ...]
// request :參數傳遞過程中的變數。[用來控制翻頁時的pages變數]
public ResultSet querySql(String Countsql, String Pagisql,
HttpServletRequest request) throws Exception {
HttpFile = request.getRequestURI(); // 擷取當前檔案名稱。
nowPage = request.getParameter("pages"); // 擷取當前頁,將數值賦予intPage變數。[分頁欄中必須要有pages參數]
if (nowPage == null) {
intPage = 1;
} else {
intPage = Integer.parseInt(nowPage);
if (intPage < 1)
intPage = 1;
}
CountTopicrs = db.executeQuery(Countsql); // @@@@@@@@@@@@擷取總記錄數的結果集。
if (CountTopicrs.next()) {
intCountTopic = CountTopicrs.getInt(1);
}
intPageCount = (intCountTopic + intPageSize - 1) / intPageSize; // 擷取總頁數。
if (intPage > intPageCount)// 如果當前頁大於總頁數,則當前頁等於總頁數。
{
intPage = intPageCount;
}
CountTopicrs.close(); // 關閉總主題數的資料集。
db.close_all();
Pagirs = db.executeQuery(Pagisql); // @@@@@@@@@@@@@@@擷取執行分頁的結果集。
return Pagirs;
}// end querySql function.


public int getCountTopic()// 擷取記錄總數。
{
return intCountTopic;
}


public int getPageCount() // 擷取總頁數。
{
return intPageCount;
}


public int getIntPage() // 擷取當前頁數。
{
return intPage;
}


public String PageFooter() {
String str = "";
int next, prev;
prev = intPage - 1;
next = intPage + 1;
str += "查詢到<font color=red>" + getCountTopic() + "</font>條記錄"
+ "    共<font color=red>" + getPageCount() + "</font>頁";
str += " 第<font color=red>" + getIntPage() + "</font>頁 ";
if (intPage > 1)
str += " <A href=" + HttpFile + "?pages=1" + ">首頁</A> ";
else
str += " 首頁 ";
if (intPage > 1)
str += " <A href=" + HttpFile + "?pages=" + prev + ">上一頁</A> ";
else
str += " 上一頁 ";
if (intPage < intPageCount)
str += " <A href=" + HttpFile + "?pages=" + next + ">下一頁</A> ";
else
str += " 下一頁 ";
if (intPageCount > 1 && intPage != intPageCount)
str += " <A href=" + HttpFile + "?pages=" + intPageCount
+ ">尾頁</A>";
else
str += " 尾頁 ";
return str;
}


public void close_all() {
db.close_all();
}


}

DB類檔案:

package lytjb;
import java.sql.*;
//一個用於尋找資料來源的工具類。
public class DB {
private Connection con = null;
private Statement stmt = null;
ResultSet rs = null;


public ResultSet executeQuery(String sql) throws Exception {

try{  
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=SHB","sa","1234567");  
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
//con=DriverManager.getConnection("jdbc:odbc:JDBCSQLDemo_JSPTest");  
stmt=con.createStatement();  
rs=stmt.executeQuery(sql);
}catch(Exception e){}
return rs;
}


// 執行Insert,Update語句
public void executeUpdate(String sql) throws Exception {
try{  
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=SHB","sa","1234567");  
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
//con=DriverManager.getConnection("jdbc:odbc:JDBCSQLDemo_JSPTest");  
stmt=con.createStatement();  
int rs=stmt.executeUpdate(sql);

}catch(Exception e){}
}


// 關閉stmt和關閉串連
public void close_all() {
try {
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}


}

jsp顯示代碼如下:

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="GBK"%>
<%@ page import="lytjb.fenye"%>




<html>
  <head>
    <link rel="stylesheet" type="text/css" href="style.css">
  </head>
  
  <body bgcolor="c0ffc0">
  <jsp:useBean id="pagi" scope="page" class="lytjb.fenye"></jsp:useBean>
 
  
  <%
  String college="政法 ";
  String user="";  
  user = (String)session.getAttribute("username_session");  
  if(user==null || user.equals("")){ 
  user="遊客"; 
  } 
  else 
  ; 
  //out.println(user); 
   %>
      <table  align="center"  border='0'>
      <tr><td><img width="800" height="200" src="source1/1.jpg"/> </td></tr>
      </table> 
      <table  align="center"  border='1' bordercolor="c0ff00" bgcolor="" rules=rows>
      <tr>
      <td>您好<a href="modi_person_info.jsp"><%=user %></a>,歡迎光臨!請</td>
      <td><table><tr><td><input type="button" value="登陸"  onclick="window.location.href='denglu.html'"></td></tr></table></td>
      <td><table><tr><td><input type="button" onclick="window.location.href='zhuceqian.jsp'" value="註冊"/></td></tr></table></td>
      <td><input type="button" value="退出" name="logout" onclick="window.location.href='tuichu.jsp'"/></td>
      <td><table><tr><td width="360"><input type="button" value="管理員登陸" onclick="window.location.href='admin_denglu.html'"></td></tr></table></td>
      <td>
      <table align="right"><tr><td><input type="button" onclick="window.location.href='shopCar.jsp'" value="我的購物車" id="shopCar"/></td></tr></table>
      </td>
      </tr>
    </table>
   <table width="800"  align="center" border=1 height="40" bordercolor="c0ff00" bgcolor="ffffff">
    <tr><td width="200"  align="center"><a href="index.jsp">首頁</a></td><td width="200" align="center"><a href="er_shou_shu.jsp">二手書</a></td><td width="200"  align="center"><a href="liu_yan_ban.jsp">留言板</a></td><td width="200" align="center"><a href="lian_xi_wo_men.jsp">聯絡我們</a></td></tr> 
   </table> 
    
   <p></p>
   <table align="center"  >
   <tr><td align="center" ><form name="serach_college_book" action="sousuoshuji.jsp?college=<%=college %>" method="get">
    <input type="text" name="college_book_search" id="college_book_text"/>
    &nbsp;&nbsp;<input type="submit" name="college_book_search" id="college_book_submit" value="搜尋"/>
   </form></td></tr>
   </table>
   <!--table開始顯示圖書-->
   
   <table align="center">
  <tr>


    <%
String CountQuery = "select count(*) from college_info where college='政法 '";//查詢總記錄數
String sql="select book_picture,book_name,book_out_price,book_stock from college_info where college='政法 '";//查詢結果集
ResultSet rs = pagi.querySql(CountQuery, sql, request);//request為jsp內建變數
String footer = pagi.PageFooter();//這句話一定要寫在while之前,否則頁面擷取到的記錄數為0
if (pagi.intPageCount > 0) {
int i = 0;
while (rs.next()) {
i++;
if (i > ((pagi.intPage - 1) * pagi.intPageSize)
&& (i <= pagi.intPage * pagi.intPageSize)) {
String picture=rs.getString("book_picture");
    String bookname=rs.getString("book_name");
    String price=rs.getString("book_out_price");
    String no=rs.getString("book_stock");
    int no_int=Integer.parseInt(no);
    if(no_int<=0)
    no="缺貨";
   


    %>
<td><table  align="center" ><tr ><td align="center" width="163"><img width="100" height="120" src="source/<%=picture %>.jpg"/></td></tr><tr width="100"><td align="center" width="100"><a href="detail.jsp?bookid=<%=picture %>"><%=bookname %></a></td></tr><tr><td align="center">價格:<%=price %></td></tr><tr><td align="center">剩餘量:<%=no %></td></tr></table></td>
   
    <% 
    i=i+1; 
    if(i%5==0){
    out.println("</tr><tr>");
    }
    %>

<%
}
}
}
out.println("<table width='700' align=" + "right"
+ "><tr><td colspan=2>" + footer + "</td></tr></table>");//翻頁顯示一定要寫在資料庫關閉之前
rs.close();
//pagi.close_all();
%>
</tr>
  </table>
  
  </body>
</html>

如下:


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.