Struts2 web层 分页解决方案

    技术2022-05-19  23

    首先,两个工具类:

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

    import java.util.List;      public class TotalRecord {           private List entities;    //总条数    private long totalCount=0;        //当前页    private int currentPage=1;    //每页显示数量    private int pageSize = 10;    //总页数    private long totalPage=0;        public TotalRecord(){         }    public TotalRecord(int currentPage){     this.currentPage = currentPage;         }        public TotalRecord(int currentPage,int pageSize){     this.currentPage = currentPage;     this.pageSize = pageSize;    }       public TotalRecord(int currentPage,int pageSize,int totalCount ){     this.currentPage = currentPage;     this.pageSize = pageSize;     this.totalCount=totalCount;    }    public List getEntities() {     return entities;    }    public void setEntities(List entities) {     this.entities = entities;    }    public long getTotalCount() {     return totalCount;    }    public void setTotalCount(long totalCount) {     this.totalCount = totalCount;    }    public int getFirstIndex() {        return (currentPage - 1)*pageSize;    }        public int getCurrentPage() {     return currentPage;    }    public void setCurrentPage(int currentPage) {     this.currentPage = currentPage;    }    public int getPageSize() {     return pageSize;    }    public void setPageSize(int pageSize) {     this.pageSize = pageSize;    }    public long getTotalPage() {        this.totalPage = this.totalCount%pageSize==0?this.totalCount/pageSize:this.totalCount/pageSize+1;        return this.totalPage;    }       }  

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

    public class PageIndex {       private long startindex;       private long endindex;              public PageIndex(long startindex, long endindex) {           this.startindex = startindex;           this.endindex = endindex;       }       public long getStartindex() {           return startindex;       }       public void setStartindex(long startindex) {           this.startindex = startindex;       }       public long getEndindex() {           return endindex;       }       public void setEndindex(long endindex) {           this.endindex = endindex;       }       /**       *        * @param viewpagecount 显示页码数       * @param currentPage 当前页       * @param totalpage 总页数       * @return       */       public static PageIndex getPageIndex(long viewpagecount, int currentPage, long totalpage){           long startpage = currentPage-(viewpagecount%2==0? viewpagecount/2-1 : viewpagecount/2);           long endpage = currentPage+viewpagecount/2;           if(startpage<1){               startpage = 1;               if(totalpage>=viewpagecount) endpage = viewpagecount;               else endpage = totalpage;           }           if(endpage>totalpage){               endpage = totalpage;               if((endpage-viewpagecount)>0) startpage = endpage-viewpagecount+1;               else startpage = 1;           }           return new PageIndex(startpage, endpage);            }   }     

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

    <%@ page language="java" pageEncoding="UTF-8"%>       <a href="javascript:toPage(1)" mce_href="javascript:toPage(1)">首页</a>           <s:if test="#request.tr.currentPage!=1">           <a href='javascript:toPage(<s:property value="#request.tr.currentPage-1"></a>)'>上一页</a>           </s:if>           <s:bean name="org.apache.struts2.util.Counter" id="counter">           <s:param name="first" value="#request.pi.startindex" />           <s:param name="last" value="#request.pi.endindex" />           <s:iterator>           <s:if test="#counter.current-1==#request.tr.currentPage">               <s:property/>           </s:if>           <s:else>               <a href="javascript:toPage(<s:property></a>)"><s:property/></a>           </s:else>           </s:iterator>           </s:bean>           <s:if test="#request.tr.currentPage!=#request.tr.totalPage">           <a href='javascript:toPage(<s:property value="#request.tr.currentPage+1"></a>)'>下一页</a>           </s:if>       <a href="javascript:toPage(<s:property value=" mce_href="javascript:toPage(<s:property value="#request.tr.totalPage"></a>)">尾页</a>             

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

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

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

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

    action代码:

    public String getUsers(){           int page =1;           if(request.getParameter("page")!=null&&request.getParameter("page")!=""){               page =Integer.parseInt(request.getParameter("page").toString());           }                      TotalRecord tr = new TotalRecord();           //tr.setEntities(list);           tr.setCurrentPage(page);           tr.setPageSize(5);           tr.setEntities(userbiz.getUsers(tr));           tr.setTotalCount(100);           PageIndex pi = PageIndex.getPageIndex(10, tr.getCurrentPage(),tr.getTotalPage());           request.setAttribute("tr", tr);           request.setAttribute("pi", pi);           return SUCCESS;                  }  

    最新回复(0)