欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-03 20:35:34
本文实例为大家分享了java分页展示的具体代码,供大家参考,具体内容如下 先上图吧,大致如图,也就提供个思路(ps:使用了ssh框架) 前台jsp页面...

本文实例为大家分享了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">«</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>/ ${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">»</span>
         </a>
        </li>
       </s:if>
       <li>
        <span class="skippagespan">跳转到第 
        <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>
         页
        </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();
 }

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