普通jsp table分页

本文介绍如何在jsp中实现表格数据的分页显示,包括Pager部分的代码实现和action中的SQL语句处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个普通的分页没有用到任何前台的框架,因为我要做一个比较恶心的项目,必须让IE6支持,我试了一下其他的都不怎么好使,只能自己手动的写一个分页,这里只用功能,关于样式什么时候用到再稍微的修改修改吧。
首先来着大致的的图片:

下面是分页
接下来我们开始看它的代码

   </div>
          <div class="row">
            <div class="col-md-12">
              <div class="table-responsive">
                <table class="table table-striped table-hover table-bordered">
                  <thead>
                    <tr align="center">
                      <th>序号</th>
                      <th>系统名称</th>
                      <th>所属单位</th>
                    </tr>
                    <tr>
                    </tr> 
                    </thead>
                    <tbody>
                        **<s:iterator value="list" var="li">**
                        <tr class="success" align="center">
                          <td>年号</td>
                          <td><s:property value="#li.appid"/></td>
                          <td><s:property value="#li.menue"/></td>
                        </tr>
                        **</s:iterator>**
                  </tbody>
<分页部分>
**<tr ><td colspan="17" align="center">
 <input type="hidden" id="perPagerRows" value="${pager.perPageRows}">//每次刷新的时候js给select做选中
//select的onchange是点击之后就提交,相当于前面加了一个a标签      
<span>每页显示 
 <select id="selectOp" onchange="self.location.href=options[selectedIndex].value">
  <option id="10" value="<%= basePath%>JRXTAction.action?pager.perPageRows=10">10</option>
  <option id="20" value="<%= basePath%>JRXTAction.action?pager.perPageRows=20">20</option>
  <option id="50" value="<%= basePath%>JRXTAction.action?pager.perPageRows=50">50</option>
  <option id="100" value="<%= basePath%>JRXTAction.action?pager.perPageRows=100">100</option>
  <option id="200" value="<%= basePath%>JRXTAction.action?pager.perPageRows=200">200</option>
  <option id="1000" value="<%= basePath%>JRXTAction.action?pager.perPageRows=1000">1000</option>
 </select>条记录
</span>
     <s:if test="%{pager.curPage>1}">
          <a href='JRXTAction.action?pager.curPage=1'>
             <span >首页</span>
          </a>

         <a href='JRXTAction.action?pager.curPage=${pager.curPage-1 }'>
            <span>上一页</span>
         </a>
     </s:if>
     <span>共 ${pager.rowCount}记录,共${pager.curPage}/${pager.pageCount}页</span>
     <s:if test="pager.curPage < pager.pageCount">
        <a href="JRXTAction.action?pager.curPage=${pager.curPage+1}">
            <span>下一页</span>
        </a>
        <a href='JRXTAction.action?pager.curPage=${pager.pageCount }'>
            <span>尾页</span>
        </a>
    </s:if>
</td></tr></table>**

            </div>
          </div>
        </div>

Pager部分

public class Pager {
    private int curPage;//当前第几页
    private int perPageRows ;//每页显示的记录数
    private int rowCount; //记录数据总数
    private int pageCount; //总页数
    省略setter与getter方法
}

action部分(此处将sql语句也写一起)

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import DataCountView.JRXTEntity;
import com.hcycom.dao.JDBCUtil;
import com.opensymphony.xwork2.ActionSupport;

public class JRXTAction extends ActionSupport{
    private static final long serialVersionUID = 1L;
    private Pager pager;
    private List<JRXTEntity> list;
    //此处省略setter和getter方法
    public Connection lj() {
        try {
            Connection conn = JDBCUtil.getConnection();
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public void close(ResultSet rs, Statement sql,   Connection conn) {
        JDBCUtil.closeResource(rs, sql, conn);
    }

    public String getJRXTEntity1(){
        int curPage=1;
        int perPageRows=10;
        if(pager!=null){//这是防止刷新页面时候进来if语句,会出现只有pager一个属性有值,其他没有值会出现0的情况
            System.out.println("pager not null");
            if(pager.getCurPage()!=0){
                curPage=pager.getCurPage();
            }
            if(pager.getPerPageRows()!=0){
                perPageRows = pager.getPerPageRows();
            }
        }
        System.out.println("pager.getPerPageRows():"+perPageRows+"cur:"+curPage);
        int count=countJRXTEntity();//查出来数据总共条数

         list = new ArrayList<JRXTEntity>();
        try {
            Connection conn1 = lj();
            Statement sql1 = conn1.createStatement();
            ResultSet rq = sql1.executeQuery("select * from rkrm$_log_taskinfo limit "+(curPage-1)*perPageRows+","+perPageRows+""); //limit后两个参数是第几条数据开始查,每页显示几条数据。
            while (rq.next()) {
                JRXTEntity jrxtEntity = new JRXTEntity();
                jrxtEntity.setAppid(rq.getString(1));
                jrxtEntity.setMenue(rq.getString(2));
                jrxtEntity.setMenuid(rq.getString(3));
                list.add(jrxtEntity);
            }
            close(rq, sql1, conn1);

            Pager page=new Pager();
            //设置pager对象中的perPageRows属性,表示每页显示的记录数
            page.setPerPageRows(perPageRows);
            //设置pager对象中的rowCount属性,表示记录总数
            page.setRowCount(count);
            page.setCurPage(curPage);
            if(count%perPageRows!=0){//计算有几页
                page.setPageCount(count/perPageRows+1);
            }else {
                page.setPageCount(count/perPageRows);
            }
            pager=page;//将查出来的值放在pager实体里面
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "aaaaa";
    }


}
package com; public class Pager { private int totalRows = 0; // 记录总数 private int totalPages = 0; // 总页数 private int pageSize = 10; // 每页显示数据条数,默认为10条记录 private int currentPage = 1; // 当前页数 private boolean hasPrevious = false; // 是否有上一页 private boolean hasNext = false; // 是否有下一页 public int getSearchFrom() { return (currentPage - 1) * pageSize; } public Pager() { } public void init(int totalRows) { this.totalRows = totalRows; this.totalPages = ((totalRows + pageSize) - 1) / pageSize; refresh(); // 刷新当前页面信息 } /** * * @return Returns the currentPage. * */ public int getCurrentPage() { return currentPage; } /** * * @param currentPage * current page * */ public void setCurrentPage(int currentPage) { this.currentPage = currentPage; refresh(); } /** * * @return Returns the pageSize. * */ public int getPageSize() { return pageSize; } /** * * @param pageSize * The pageSize to set. * */ public void setPageSize(int pageSize) { this.pageSize = pageSize; refresh(); } /** * * @return Returns the totalPages. * */ public int getTotalPages() { return totalPages; } /** * * @param totalPages * The totalPages to set. * */ public void setTotalPages(int totalPages) { this.totalPages = totalPages; refresh(); } /** * * @return Returns the totalRows. * */ public int getTotalRows() { return totalRows; } /** * * @param totalRows * The totalRows to set. * */ public void setTotalRows(int totalRows) { this.totalRows = totalRows; refresh(); } // 跳到第一页 public void first() { currentPage = 1; this.setHasPrevious(false); refresh(); } // 取得上一页(重新设定当前页面即可) public void previous() { if (currentPage > 1) { currentPage--; } refresh(); } // 取得下一页 public void next() { //System.out.println("next: totalPages: "
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值