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

 更新时间:2017年03月17日 08:20:39   作者:dKnightL  
这篇文章主要为大家详细介绍了Java实现分页的前台页面和后台代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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();
 }

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

相关文章

  • Apache Maven创建工程的实现示例

    Apache Maven创建工程的实现示例

    本文详细介绍了如何使用Maven创建一个新的Java工程,包括使用maven-archetype-plugin插件、项目的基本结构和文件、构建和运行项目的方法以及常见问题的解决,Maven通过简化项目构建和依赖管理,成为Java开发中不可或缺的工具
    2024-11-11
  • Maven指定JDK版本的实现

    Maven指定JDK版本的实现

    本文主要介绍了Maven指定JDK版本的实现,主要有两种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-09-09
  • java中几种常见的排序算法总结

    java中几种常见的排序算法总结

    大家好,本篇文章主要讲的是java中几种常见的排序算法总结,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-01-01
  • Java实战入门之双色球彩票小游戏

    Java实战入门之双色球彩票小游戏

    这篇文章主要介绍了Java实战入门之双色球彩票,文中有非常详细的代码示例,对正在学习java的小伙伴们有非常好的帮助,需要的朋友可以参考下
    2021-04-04
  • java查找文件夹下最新生成的文件的方法

    java查找文件夹下最新生成的文件的方法

    在本篇文章中我们给大家分享了关于java怎么查找文件夹下最新生成的文件的相关方法和知识点,有需要的朋友们参考下。
    2019-07-07
  • Java特性队列和栈的堵塞原理解析

    Java特性队列和栈的堵塞原理解析

    这篇文章主要介绍了Java特性队列和栈的堵塞原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-10-10
  • java持久层框架mybatis防止sql注入的方法

    java持久层框架mybatis防止sql注入的方法

    下面小编就为大家带来一篇java持久层框架mybatis防止sql注入的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-10-10
  • MyBatis动态创建表的实例代码

    MyBatis动态创建表的实例代码

    在项目需求中,我们经常会遇到动态操作数据表的需求,常见的我们会把日志、设备实时位置信息等存入数据表,并且以一定时间段生成一个表来存储。接下来通过本文给大家介绍MyBatis动态创建表的方法,感兴趣的朋友一起看看吧
    2018-07-07
  • mybatis-plus 插入修改配置默认值的实现方式

    mybatis-plus 插入修改配置默认值的实现方式

    这篇文章主要介绍了mybatis-plus 插入修改配置默认值的实现方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • Java中File文件操作类的基础用法

    Java中File文件操作类的基础用法

    这篇文章主要给大家介绍了关于Java中File文件操作类基础用法的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用File类具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-07-07

最新评论