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

作者:dKnightL 时间:2021-07-22 17:10:04 

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

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

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

前台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,分页
0
投稿

猜你喜欢

  • TC 集群Seata1.6高可用架构源码解析

    2022-04-18 05:02:34
  • Android UI 实现老虎机详解及实例代码

    2022-06-09 05:01:30
  • C#解析json字符串总是多出双引号的原因分析及解决办法

    2022-10-22 02:40:46
  • LZ77压缩算法原理的理解

    2023-01-16 17:48:02
  • 基于java实现画图板功能

    2022-12-09 07:04:49
  • Kotlin中常见内联扩展函数的使用方法教程

    2023-07-04 13:46:12
  • Mybatis实现插入数据后返回主键过程解析

    2022-07-14 18:43:15
  • java构造方法的作用总结

    2023-05-31 00:54:45
  • Mybatis中的延迟加载案例解析

    2023-02-27 01:55:37
  • c++传递函数指针和bind的示例

    2021-06-18 18:34:38
  • Android使用Websocket实现聊天室

    2023-07-07 13:01:59
  • Android开发ImageView图片无法显示解决过程

    2023-06-07 21:34:39
  • c#求两个数中最大值的方法

    2021-09-21 15:24:20
  • JavaWeb开发之【Tomcat 环境配置】MyEclipse+IDEA配置教程

    2022-05-04 12:50:18
  • SpringMVC RESTFul及REST架构风格介绍

    2021-09-30 05:05:33
  • Spring Boot + Vue 前后端分离开发之前端网络请求封装与配置

    2021-05-28 02:48:41
  • c#操作Redis的5种基本类型汇总

    2021-06-03 03:24:07
  • OpenFeign设置header的三种方式总结

    2023-06-25 19:03:46
  • 详解Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用

    2021-11-18 12:18:05
  • 浅谈关于Java的GC垃圾回收器的一些基本概念

    2021-11-14 10:42:52
  • asp之家 软件编程 m.aspxhome.com