• jsp页面数据分页模仿百度分页效果


    <%@page import="web09.shop.DBUtil"%>
    <%@page import="java.sql.ResultSet"%>
    <%@page import="java.sql.PreparedStatement"%>
    <%@page import="java.sql.Connection"%>
    <%@ page language="java" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>数据分页</title>
        <style type="text/css">
            .page a{
                min- 34px;
                height: 34px;
                border: 1px solid #e1e2e3;
                display:block;
                margin:0 3px;
                float: left;
                text-decoration: none;
                text-align:center;
                line-height: 34px;            
            }
            
            .page a:HOVER {
                background: #f2f8ff;
                border: 1px solid #38f ;
            }
            .page a.prev{
                90px;
            }
            .page span{
                 34px;
                height: 34px;
                border: 1px solid transparent;
                cursor: pointer;
                display:block;
                float: left;
                text-decoration: none;
                text-align:center;
                line-height: 34px;
                cursor: default;
            }
        </style>
    </head>
    
    <body>
    <table class="tt" border="1" align="center" width="80%" cellpadding="10">
        <tr>
            <th>ID</th>
            <th>姓名</th>
            <th>年龄</th>
            <th>专业</th>
        </tr>
        <%
        DBUtil dbutil=new DBUtil();
        Connection conn=dbutil.getCon();
        //Connection conn = new DBUtil().getCon();
        PreparedStatement pstmt1 = conn.prepareStatement("select count(*) from student");
        ResultSet rs1 = pstmt1.executeQuery();
        rs1.next();
        int recordCount = rs1.getInt(1);     //记录总数
        int pageSize = 10;                    //每页记录数
        int start=1;                        //显示开始页
        int end=10;                            //显示结束页
        int pageCount = recordCount%pageSize==0 ? recordCount/pageSize : recordCount/pageSize+1; 
        int currPage = request.getParameter("p")==null ? 1 : Integer.parseInt(request.getParameter("p"));
        
        currPage = currPage<1 ? 1 : currPage;
        currPage = currPage>pageCount ? pageCount : currPage;
        
        PreparedStatement pst = conn.prepareStatement("select * from student limit ?,?");
        pst.setInt(1,currPage*pageSize-pageSize);
        pst.setInt(2,pageSize);
        ResultSet rs = pst.executeQuery();
        
        while(rs.next()){
        %>
        <tr align="center">
        <td><%=rs.getInt(1) %></td>
        <td><%=rs.getString(2) %></td>
        <td><%=rs.getInt("age") %></td>
        <td><%=rs.getString(4) %></td>
        </tr>
        <%
        }
        %>
        <tr>
              <th colspan="4" class="page">
                  <% 
                      
                      if(currPage>=7){
                          start=currPage-5;
                          end=currPage+4;
                      }
                      if(start>(pageCount-10)){
                          start=pageCount-9;
                      }
                      if(currPage>1){
                              out.print(String.format("<a class="prev" href="?p=%d">首页</a>",1));
                              out.print(String.format("<a class="prev" href="?p=%d">&lt;&lt;上一页</a>",currPage-1));
                      }
                      
                      for(int i=start;i<=end;i++){
                          if(i>pageCount) break;
                          String pageinfo=String.format("<a href="?p=%d">%d</a>",i,i);
                          if(i==currPage){
                              pageinfo=String.format("<span>%d</span>",i);
                          }
                          out.print(pageinfo);
                      }
                      
                      if(currPage<pageCount){
                          out.print(String.format("<a class="prev" href="?p=%d">下一页&gt;&gt;</a>",currPage+1));
                          out.print(String.format("<a class="prev" href="?p=%d">尾页</a>",pageCount)); 
                      }
                      
                      
                  %>
              </th>
          </tr>
    </table>
    </body>
    </html>
    请根据自己的项目、包名、数据库信息进行修改
  • 相关阅读:
    试题 基础练习 Sine之舞
    试题 基础练习 Huffuman树
    试题 基础练习 完美的代价
    支付宝支付功能, 创建订单并生成支付链接的接口, 后端支付宝异步回调接口
    GenericAPIView, drf内置的基础分页器和偏移分页器的使用, 过滤器, django-filter插件实现区间分类
    celery, 数据库分表
    redis
    注册, 校验手机号接口, 手机号注册接口, 全局配置drf-jwt身份认证, 局部配置SendSmsAPIView类的频率认证, Vue操作Cookie
    登录, 发送验证码接口, 对官方提供的短信SDK进行二次封装
    pycharm全局搜索快捷键: ctrl + n, Xadmin的使用, 前端Banner小组件, 后端控制轮播图展示的数量, git
  • 原文地址:https://www.cnblogs.com/fqszywz/p/7162290.html
Copyright © 2020-2023  润新知