Java实现分页的前台页面和后台代码

发布时间 - 2026-01-11 00:13:07    点击率:

本文实例为大家分享了Java分页展示的具体代码,供大家参考,具体内容如下

先上图吧,大致如图,也就提供个思路(ps:使用了SSH框架)

前台JSP页面

<%@ page language="java" contentType="text/html; charset=utf-8"
 pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
 <title>数据交易猫</title> 
 <script type="text/javascript"> 

 //1分页下,动态添加disable给分页按钮
 /*
 $(function(){
  var myPageId="#"+$("#hidCurrentPage").val();
  var myPageAId="#"+$("#hidCurrentPage").val()+" a";
  $(myPageAId).addClass('main-bgcolor');
  $(myPageAId).attr('href','javascript:void(0)')
  $(myPageId).addClass('disabled');
  $(myPageId).addClass('disabledControl');

 })
 */
 //
 $(function(){

 })
 //根据页数查询数据列表
 function queryRequirListByPage(i) {
  var pageNo=i;
  var sortValue=$('#hidSortValue').val();
  $.ajax({
   url:'${pageContext.request.contextPath}/bid/reAction_queryRequirListByPage.action',
   type:'POST',
   data:{
    sortValue:sortValue,
    pageNo:pageNo
   },
   success:function(datas){ 
    $('#requireContentDiv').html(datas);          
   },
   error:function(){
    alert("失败");
   },
   });
 }

 //根据下拉查询数据列表
 function selectPage(obj){
  var pageNo=obj.options[obj.selectedIndex].value;
  var sortValue=$('#hidSortValue').val();
  $.ajax({
   url:'${pageContext.request.contextPath}/bid/reAction_queryRequirListByPage.action',
   type:'POST',
   data:{
    sortValue:sortValue,
    pageNo:pageNo
   },
   success:function(datas){ 
    $('#requireContentDiv').html(datas);          
   },
   error:function(){
    alert("失败");
   },
   });
  }
 //根据下拉选择排序方式
 function selectSort(obj){
  var sortValue = obj.options[obj.selectedIndex].value;
  var pageNo =1;
  $.ajax({
   url:'${pageContext.request.contextPath}/bid/reAction_queryRequirListByPage.action',
   type:'POST',
   data:{sortValue:sortValue,
     pageNo:pageNo
    },
   success:function(datas){ 
    $('#requireContentDiv').html(datas);


   },
   error:function(){
    alert("失败");
   },
   });
  }

  $(document).ready(function(){
   var backSortValue=$('#backSortValue').val();
   console.log("backSortValue"+backSortValue)
   $("#category option").each(function(){
    var thisId='#'+this.id;
    var thisValue=this.value;
    if(backSortValue==thisValue){
     $(thisId).attr('selected','selected');
    }
   });
  })
 </script> 
</head>
<body>

    <!-- 内容-->
    <div class="well">
    <!-- 标题-->
     <div class="box"><h3><span class="glyphicon glyphicon-list" ></span>需求列表</h3></div>
    <!-- 筛选条件--> 
     <div class="box">
      <div class="row">
       <div class="col-xs-12">        
         <span>筛选:按</span>
         <select id="category" name="category" onchange="selectSort(this)">
          <option id="categoryTime" value="publishDatetime">最新</option>
          <option id="categoryPrice" value="price">价格降序</option>
          <input id="backSortValue" type="hidden" value="${sortValue}">
         </select>
         <hr class="mrgZero mrgTopSma"/>
       </div>
      </div>
     </div>     
    <!-- 内容-->
      <input type="hidden" name="hidCurrentPage2" id="hidCurrentPage" value="${currentPage}">
      <input type="hidden" id="hidAllPage" value="${allPage}">
      <input type="hidden" id="hidSortValue" value="${sortValue}">
      <s:iterator value="#requiList">

      <div class="data-down-box">
       <div class="row">
        <div class="col-xs-12">
         <h4 class="ellipsis"><a href="${pageContext.request.contextPath}/bid/bidAction_queryById?id=${id}" rel="external nofollow" onclick="reward()">${title}</a></h4>
        </div>                         
       </div>
       <div class="row mrgTopSma">
        <div class="col-xs-12 ">      
         <p class="data-provider padLeftBig sec-color ellipsis">悬赏积分:<span>${price}</span></p>
         <p class="data-intro padLeftBig ellipsis sec-color">需求描述:<span>${requirementDescription}</span></p>
        </div>    
       </div>
       <hr/>  
      </div>

      </s:iterator>
     <!-- 分页 -->
     <div id="rePagerDiv" class="rePagerDiv box">
      <nav>
       <ul class="pager">

        <!-- 判断当前页是否位1,如果不为1则显示上一页, --> 
        <s:if test="1 == #currentPage">  
        </s:if>
        <s:else>
        <li>  
         <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" aria-label="Previous" onclick="queryRequirListByPage(${currentPage-1})">
         <span aria-hidden="true">&laquo;</span>
         </a>
        </li>  
        </s:else>

       <!-- 首页 -->        
       <li><a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="queryRequirListByPage(1)">首页</a></li>

       <li>
        <span><span class="main-color">${currentPage}</span>/&nbsp;${allPage}页</span>        
       </li>

       <!-- 尾页 -->        
       <li><a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="queryRequirListByPage(${allPage})">尾页</a></li>

       <!-- 判断当前页和总页数,小于则显示下一页, --> 
       <s:if test="#currentPage < #allPage">
        <li>
         <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" aria-label="Next" onclick="queryRequirListByPage(${currentPage+1})">
         <span aria-hidden="true">&raquo;</span>
         </a>
        </li>
       </s:if>
       <li>
        <span class="skipPageSpan">跳转到第&nbsp;
        <select onchange="selectPage(this)">
         <s:iterator var="lst" begin="1" end="#allPage" step="1">          
          <s:if test="%{#lst == #currentPage}">
           <option selected="selected" value="<s:property/>" ><s:property/></option>  
           </s:if>
           <s:else>
            <option value="<s:property/>" ><s:property/></option>
           </s:else>
         </s:iterator>                    
        </select>
        &nbsp;页
        </span>
        </li>

       </ul>
      </nav>

     </div>      


     </div>     




 <hr/>

</body>
</html>

action

 //查询需求列表
 public String queryRequirListByPage(){
  int pageSize=5;//每页记录
  String hql="select r from Requirement r where r.reStatus !=2 ";
  if(sortValue == null || sortValue.length() <= 0){
   hql=hql+"order by r.publishDatetime desc";
   ActionContext.getContext().put("sortValue", "publishDatetime"); //当前页码条件
   session.put("sessionReqSortValue","publishDatetime");
  }else{
  hql=hql+"order by r."+sortValue+" desc";
   ActionContext.getContext().put("sortValue", sortValue); //当前页码条件
   session.put("sessionReqSortValue",sortValue);
  }
  long icount=requirementService.countAllRe();//总记录数
  long allPage;//总页数
  //判断是否能整除,能则直接,不能则+1;
  if((icount%pageSize)==0){
   allPage=icount/pageSize;
  }
  else{
   allPage=(icount/pageSize)+1;
  }
  System.out.println("总记录:"+icount+";总页数:"+allPage+";当前页码:"+pageNo);
  List<Requirement> requiList=requirementService.queryByPage(hql, pageNo, pageSize);
  ActionContext.getContext().put("requiList", requiList);//需求列表
  ActionContext.getContext().put("icount", icount);//总记录数
  ActionContext.getContext().put("allPage", allPage);//总页数
  ActionContext.getContext().put("currentPage", pageNo); //当前页码
  session.put("sessionCurrentPage", pageNo);
  return "requireContent";

 }

service

  public long countAllRe() {
  return requirementDao.countAllRe();
 }
  public List<T> queryByPage(String hql, int pageNo, int pageSize) {
  return requirementDao.queryByPage(hql, pageNo, pageSize);
 }

dao

 //这里可能会报错,就是直接查询数据列表(使用了SSH)
 public long countAll() {
  List<?> l = getSession().createQuery("select count(*) from "
    + clazz.getSimpleName()).list();
  if (l != null && l.size() == 1 )
  {
   return (Long)l.get(0);
  }
  return 0;
 }
 public List<T> queryByPage(String hql, int pageNo, int pageSize) {
  return getSession()
    .createQuery(hql)
    .setFirstResult((pageNo - 1) * pageSize)
    .setMaxResults(pageSize)
    .list();
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


# Java  # 分页  # Java分页查询--分页显示(实例讲解)  # Java实现简单的分页功能  # Java实现分页代码  # java实现分页显示效果  # Java分页工具类及其使用(示例分享)  # java分页工具类的使用方法  # Java项目开发中实现分页的三种方式总结  # 尾页  # 首页  # 使用了  # 也就  # 下一页  # 上一页  # 当前页  # 每页  # 不为  # 如图  # 跳转  # 报错  # 大家分享  # 到第  # 是否能  # 具体内容  # 大家多多  # 图吧  # 降序 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: 网站页面设计需要考虑到这些问题  如何快速搭建自助建站会员专属系统?  Laravel如何安装使用Debugbar工具栏_Laravel性能调试与SQL监控插件【步骤】  什么是javascript作用域_全局和局部作用域有什么区别?  Laravel的HTTP客户端怎么用_Laravel HTTP Client发起API请求教程  高防服务器租用指南:配置选择与快速部署攻略  如何快速搭建高效可靠的建站解决方案?  Laravel如何清理系统缓存命令_Laravel清除路由配置及视图缓存的方法【总结】  网站建设要注意的标准 促进网站用户好感度!  如何在 Python 中将列表项按字母顺序编号(a.、b.、c. …)  如何破解联通资金短缺导致的基站建设难题?  Java垃圾回收器的方法和原理总结  Laravel事件监听器怎么写_Laravel Event和Listener使用教程  悟空浏览器如何设置小说背景色_悟空浏览器背景色设置【方法】  如何在腾讯云服务器快速搭建个人网站?  Win11任务栏卡死怎么办 Windows11任务栏无反应解决方法【教程】  免费制作统计图的网站有哪些,如何看待现如今年轻人买房难的情况?  Laravel如何配置.env文件管理环境变量_Laravel环境变量使用与安全管理  Android GridView 滑动条设置一直显示状态(推荐)  Linux安全能力提升路径_长期防护思维说明【指导】  *服务器网站为何频现安全漏洞?  ChatGPT 4.0官网入口地址 ChatGPT在线体验官网  公司门户网站制作流程,华为官网怎么做?  微信小程序 canvas开发实例及注意事项  Bootstrap整体框架之CSS12栅格系统  怎么制作网站设计模板图片,有电商商品详情页面的免费模板素材网站推荐吗?  html5的keygen标签为什么废弃_替代方案说明【解答】  详解一款开源免费的.NET文档操作组件DocX(.NET组件介绍之一)  香港服务器选型指南:免备案配置与高效建站方案解析  制作旅游网站html,怎样注册旅游网站?  百度浏览器ai对话怎么关 百度浏览器ai聊天窗口隐藏  EditPlus中的正则表达式 实战(1)  高性能网站服务器配置指南:安全稳定与高效建站核心方案  Gemini手机端怎么发图片_Gemini手机端发图方法【步骤】  如何在万网自助建站平台快速创建网站?  原生JS实现图片轮播切换效果  Laravel策略(Policy)如何控制权限_Laravel Gates与Policies实现用户授权  如何在七牛云存储上搭建网站并设置自定义域名?  怎么用AI帮你设计一套个性化的手机App图标?  详解jQuery中的事件  Laravel API路由如何设计_Laravel构建RESTful API的路由最佳实践  Python函数文档自动校验_规范解析【教程】  网站视频制作书签怎么做,ie浏览器怎么将网站固定在书签工具栏?  Swift中循环语句中的转移语句 break 和 continue  Laravel安装步骤详细教程_Laravel环境搭建指南  Laravel如何实现本地化和多语言支持?(i18n教程)  深圳网站制作平台,深圳市做网站好的公司有哪些?  javascript日期怎么处理_如何格式化输出  手机网站制作平台,手机靓号代理商怎么制作属于自己的手机靓号网站?  Win11搜索栏无法输入_解决Win11开始菜单搜索没反应问题【技巧】