日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

很好的分页实例代码(JSP)

發(fā)布時(shí)間:2025/6/15 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 很好的分页实例代码(JSP) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1:首先創(chuàng)建數(shù)據(jù)庫(kù)鏈接類,我的博客前面有源碼 2:創(chuàng)建封裝你要分頁(yè)的那個(gè)數(shù)據(jù)庫(kù)表的Bean 3:封裝實(shí)現(xiàn)分頁(yè)功能的功能Bean 具體代碼見下:

import java.util.ArrayList;
import java.sql.*;

public class PageBean {

????private int curPage = 1; //當(dāng)前是第幾頁(yè)
????private int maxPage; //一共有多少頁(yè)
????private int maxRowCount; //一共有多少行
????public int rowsPerPage = 10; //每頁(yè)多少行

????Connection conn;
????public ArrayList data;

????public PageBean() throws Exception {
????????this.setPageBean();
????}
????public int getCurPage() {
????????return curPage;
????}

????public int getMaxPage() {
????????return maxPage;
????}

????public int getMaxRowCount() {
????????return maxRowCount;
????}

????public int getRowsPerPage() {
????????return rowsPerPage;
????}

????public void setCurPage(int curPage) {
????????this.curPage = curPage;
????}

????public void setMaxPage(int maxPage) {
????????this.maxPage = maxPage;
????}

????public void setMaxRowCount(int maxRowCount) {
????????this.maxRowCount = maxRowCount;
????}

????public void setRowsPerPage(int rowsPerPage) {
????????this.rowsPerPage = rowsPerPage;
????}

????//得到要顯示于本頁(yè)的數(shù)據(jù)
????public PageBean getResult(String page) throws Exception {
????????try {
????????????PageBean pageBean = new PageBean();
????????????ArrayList list = new ArrayList();
????????????int pageNum = Integer.parseInt(page);
????????????Statement stmt = conn.createStatement();
????????????String strSql = "select top " + pageNum * pageBean.rowsPerPage +
????????????????????????????" * from employee";//改成你的表
????????????ResultSet rset = stmt.executeQuery(strSql);
????????????int i = 0;
????????????while (rset.next()) {

????????????????????//這里要和表的字段對(duì)應(yīng)起來(lái)!!!!
????????????????????Employee item=new Employee();
????????????????????item.setEmp_id(rset.getString("emp_id"));
????????????????????item.setFname(rset.getString("fname"));
????????????????????item.setMinit(rset.getString("minit"));
????????????????????item.setLname(rset.getString("lname"));
????????????????????item.setJob_id(rset.getInt("job_id"));
????????????????????item.setJob_lvl(rset.getInt("job_lvl"));
????????????????????item.setPub_id(rset.getString("pub_id"));
????????????????????item.setHire_date(rset.getString("hire_date"));
????????????????????list.add(item);

????????????????i++;
????????????}
????????????ConnectionManager.closeResultSet(rset);
????????????ConnectionManager.closeStatement(stmt);
????????????pageBean.setCurPage(pageNum);
????????????pageBean.data = list;
????????????return pageBean;
????????} catch (Exception e) {
????????????e.printStackTrace();
????????????throw e;

????????}
????}
????//獲取總行數(shù)
????public int getAvailableCount() throws Exception {
????????int ret = 0;
????????conn = ConnectionManager.getConnection();
????????Statement stmt = conn.createStatement();
????????String strSql = "select * from employee";//改成你的表
????????ResultSet rset = stmt.executeQuery(strSql);
????????while (rset.next()) {
????????????ret++;
????????}
????????return ret;
????}
????//初始化時(shí)對(duì)PageBean進(jìn)行設(shè)置
????public void setPageBean() throws Exception {

????????//得到總行數(shù)
????????this.setMaxRowCount(this.getAvailableCount());

????????if (this.maxRowCount % this.rowsPerPage == 0) { //根據(jù)總行數(shù)計(jì)算總頁(yè)數(shù)
????????????this.maxPage = this.maxRowCount / this.rowsPerPage;
????????} else {
????????????this.maxPage = this.maxRowCount / this.rowsPerPage + 1;
????????}
????}

}

?

4:建立中間中轉(zhuǎn)的Servlet? PageServlet: 代碼如下:

public void init() throws ServletException {
??}

??//Process the HTTP Get request
??public void doGet(HttpServletRequest request, HttpServletResponse response) throws
??????ServletException, IOException {
????try
??????{
????????????PageBean page1=new PageBean();
????????????PageBean page2=page1.getResult((String)request.getParameter("jumpPage"));

??????????????//把PageBean保存到request對(duì)象中。注意:viewpage.jsp里jsp:useBean id必須為才可以"page2"!!!
??????????????request.setAttribute("page2",page2);
??????}
??????catch(Exception e)
??????{
??????????????e.printStackTrace();
??????}

??????????/**
???????*把視圖派發(fā)到viewForum.jsp
???????*/
??????javax.servlet.RequestDispatcher dis=request.getRequestDispatcher("viewpage.jsp");
??????dis.forward(request,response);

?}

5:現(xiàn)在建立現(xiàn)實(shí)JSP頁(yè)面 viewpage.jsp:

<%@ page contentType="text/html; charset=GBK" import="java.util.*,pagetest.Employee" %>
<jsp:useBean id="page2" scope="request" class="pagetest.PageBean"/>
<script language="JavaScript" type="text/JavaScript">
function Jumping(){
??document.PageForm.submit();
??return ;
}

function gotoPage(pagenum){
??document.PageForm.jumpPage.value = pagenum;
??document.PageForm.submit();
??return ;
}
</script>
<html>
<head>
<title>
分頁(yè)功能
</title>
</head>
<body bgcolor="#ffffff">
<table border="1">
<%
String s=String.valueOf(page2.getCurPage());
try{
ArrayList list=page2.getResult(s).data;
for(int i=0;i<list.size();i++){
??Employee emp=(Employee)list.get(i);
%>
<tr>
<td><%=emp.getEmp_id()%></td>
<td><%=emp.getFname()%></td>
<td><%=emp.getHire_date()%></td>
<td><%=emp.getJob_id()%></td>
<td><%=emp.getJob_lvl()%></td>
<td><%=emp.getLname()%></td>
<td><%=emp.getMinit()%></td>
<td><%=emp.getPub_id()%></td>
</tr>
<%
?}
}catch(Exception e){}
%>
</table>
<%if(page2.getMaxPage()!=1){ %>
<form name="PageForm" action="pageservlet" method="post">
每頁(yè)
<%=page2.rowsPerPage%>


<%=page2.getMaxRowCount()%>


<%=page2.getCurPage()%>
頁(yè)

<%=page2.getMaxPage()%>頁(yè)
<BR>
<%
??if (page2.getCurPage() == 1) {
????out.print(" 首頁(yè) 上一頁(yè)");
??}
??else {
%>
<a HREF="javascript:gotoPage(1)">首頁(yè)</A>
<a HREF="javascript:gotoPage(<%=page2.getCurPage()-1%>)">上一頁(yè)</A>
<%}%>
<%
??if (page2.getCurPage() == page2.getMaxPage()) {
????out.print("下一頁(yè) 尾頁(yè)");
??}
??else {
%>
<a HREF="javascript:gotoPage(<%=page2.getCurPage()+1%>)">下一頁(yè)</A>
<a HREF="javascript:gotoPage(<%=page2.getMaxPage()%>)">尾頁(yè)</A>
<%}%>
轉(zhuǎn)到第
<SELECT name="jumpPage" onchange="Jumping()">
<%
??for (int i = 1; i <= page2.getMaxPage(); i++) {
????if (i == page2.getCurPage()) {
%>
??<OPTION selected value="<%=i%>"><%=i%> </OPTION>
<%} else {%>
??<OPTION value="<%=i%>"><%=i%> </OPTION>
<%}
??}%>
</SELECT>
頁(yè)
</form>
<%}%>
</body>
</html>

?

現(xiàn)在運(yùn)行 就可以看到效果

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的很好的分页实例代码(JSP)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。