Struts2 web层 分页解决方案

本文介绍了一种基于Java的分页查询实现方案,通过自定义`TotalRecord`和`PageIndex`工具类,实现了数据的分页展示及导航功能。该方案适用于Web应用中的大量数据分页展示场景。

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

首先,两个工具类:

第一个TotalRecord,用来存放分页条件和查询出的数据

import java.util.List;  
  1.   
  2. public class TotalRecord {  
  3.   
  4.    
  5.  private List entities;  
  6.  //总条数  
  7.  private long totalCount=0;  
  8.    
  9.  //当前页  
  10.  private int currentPage=1;  
  11.  //每页显示数量  
  12.  private int pageSize = 10;  
  13.  //总页数  
  14.  private long totalPage=0;  
  15.    
  16.  public TotalRecord(){  
  17.     
  18.  }  
  19.  public TotalRecord(int currentPage){  
  20.   this.currentPage = currentPage;  
  21.     
  22.  }  
  23.    
  24.  public TotalRecord(int currentPage,int pageSize){  
  25.   this.currentPage = currentPage;  
  26.   this.pageSize = pageSize;  
  27.  }  
  28.     public TotalRecord(int currentPage,int pageSize,int totalCount ){  
  29.   this.currentPage = currentPage;  
  30.   this.pageSize = pageSize;  
  31.   this.totalCount=totalCount;  
  32.  }  
  33.  public List getEntities() {  
  34.   return entities;  
  35.  }  
  36.  public void setEntities(List entities) {  
  37.   this.entities = entities;  
  38.  }  
  39.  public long getTotalCount() {  
  40.   return totalCount;  
  41.  }  
  42.  public void setTotalCount(long totalCount) {  
  43.   this.totalCount = totalCount;  
  44.  }  
  45.  public int getFirstIndex() {  
  46.      return (currentPage - 1)*pageSize;  
  47.  }  
  48.    
  49.  public int getCurrentPage() {  
  50.   return currentPage;  
  51.  }  
  52.  public void setCurrentPage(int currentPage) {  
  53.   this.currentPage = currentPage;  
  54.  }  
  55.  public int getPageSize() {  
  56.   return pageSize;  
  57.  }  
  58.  public void setPageSize(int pageSize) {  
  59.   this.pageSize = pageSize;  
  60.  }  
  61.  public long getTotalPage() {  
  62.      this.totalPage = this.totalCount%pageSize==0?this.totalCount/pageSize:this.totalCount/pageSize+1;  
  63.      return this.totalPage;  
  64.  }  
  65.    
  66. }  

第二个PageIndex,存放分页工具条信息

  1. public class PageIndex {  
  2.     private long startindex;  
  3.     private long endindex;  
  4.       
  5.     public PageIndex(long startindex, long endindex) {  
  6.         this.startindex = startindex;  
  7.         this.endindex = endindex;  
  8.     }  
  9.     public long getStartindex() {  
  10.         return startindex;  
  11.     }  
  12.     public void setStartindex(long startindex) {  
  13.         this.startindex = startindex;  
  14.     }  
  15.     public long getEndindex() {  
  16.         return endindex;  
  17.     }  
  18.     public void setEndindex(long endindex) {  
  19.         this.endindex = endindex;  
  20.     }  
  21.     /** 
  22.      *  
  23.      * @param viewpagecount 显示页码数 
  24.      * @param currentPage 当前页 
  25.      * @param totalpage 总页数 
  26.      * @return 
  27.      */  
  28.     public static PageIndex getPageIndex(long viewpagecount, int currentPage, long totalpage){  
  29.         long startpage = currentPage-(viewpagecount%2==0? viewpagecount/2-1 : viewpagecount/2);  
  30.         long endpage = currentPage+viewpagecount/2;  
  31.         if(startpage<1){  
  32.             startpage = 1;  
  33.             if(totalpage>=viewpagecount) endpage = viewpagecount;  
  34.             else endpage = totalpage;  
  35.         }  
  36.         if(endpage>totalpage){  
  37.             endpage = totalpage;  
  38.             if((endpage-viewpagecount)>0) startpage = endpage-viewpagecount+1;  
  39.             else startpage = 1;  
  40.         }  
  41.         return new PageIndex(startpage, endpage);         
  42.   }  
  43. }  
  

然后,是页面上的分页工具条,这里用一个jsp页面来做,使用的时候用<%@ include file="/util/PageBar.jsp" %>来将分页工具条包含在页面里:

  1. <%@ page language="java" pageEncoding="UTF-8"%>  
  2.     <a href="javascript:toPage(1)" mce_href="javascript:toPage(1)">首页</a>  
  3.         <s:if test="#request.tr.currentPage!=1">  
  4.         <a href='javascript:toPage(<s:property value="#request.tr.currentPage-1"></a>)'>上一页</a>  
  5.         </s:if>  
  6.         <s:bean name="org.apache.struts2.util.Counter" id="counter">  
  7.         <s:param name="first" value="#request.pi.startindex" />  
  8.         <s:param name="last" value="#request.pi.endindex" />  
  9.         <s:iterator>  
  10.         <s:if test="#counter.current-1==#request.tr.currentPage">  
  11.             <s:property/>  
  12.         </s:if>  
  13.         <s:else>  
  14.             <a href="javascript:toPage(<s:property></a>)"><s:property/></a>  
  15.         </s:else>  
  16.         </s:iterator>  
  17.         </s:bean>  
  18.         <s:if test="#request.tr.currentPage!=#request.tr.totalPage">  
  19.         <a href='javascript:toPage(<s:property value="#request.tr.currentPage+1"></a>)'>下一页</a>  
  20.         </s:if>  
  21.     <a href="javascript:toPage(<s:property value=" mce_href="javascript:toPage(<s:property value="#request.tr.totalPage"></a>)">尾页</a>  
  22.           

工具条里面,翻页会用到javascript方法:toPage,此方法写在页面里,因为不同的页面处理跳转可能会有不同。

  1. function toPage(currentPage){  
  2.                 var form = document.forms[0];  
  3.                 form.page.value = currentPage;  
  4.                 form.submit();  
  5.             }  

toPage方法使页面上的某个form提交数据,把页码作为参数传入后台action中

<form action="user_getUsers" method="post">
      <input type="hidden" name="page"/>
  </form>

action代码:

  1. public String getUsers(){  
  2.         int page =1;  
  3.         if(request.getParameter("page")!=null&&request.getParameter("page")!=""){  
  4.             page =Integer.parseInt(request.getParameter("page").toString());  
  5.         }  
  6.           
  7.         TotalRecord tr = new TotalRecord();  
  8.         //tr.setEntities(list);  
  9.         tr.setCurrentPage(page);  
  10.         tr.setPageSize(5);  
  11.         tr.setEntities(userbiz.getUsers(tr));  
  12.         tr.setTotalCount(100);  
  13.         PageIndex pi = PageIndex.getPageIndex(10, tr.getCurrentPage(),tr.getTotalPage());  
  14.         request.setAttribute("tr", tr);  
  15.         request.setAttribute("pi", pi);  
  16.         return SUCCESS;  
  17.           
  18.     }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值