zoukankan      html  css  js  c++  java
  • S2SH项目实现分页功能

    javaWEB项目实现分页的方法很多,网上也有很多列子,最近工作中S2SH框架项目中需要一个分页的功能,查看了很多用一下方式实现,功能思路很清晰,觉得是很好的一种实现方法,记录下便多学习。

          刚开始得到分页循环页数,但增加了跳转功能时,在select下拉框中总是重复循环最大页,通过查看代码修改,最终终于实现。解决思路就是需要增加一个跳转分页的javaBean,目的是封装成一个list,在得到页数后,在页面可以直接通过list的属性获取递增循环页数。

    1.javaBean:

    pageBean(分页类)

    1. public class PageBean {  
    2.     private List<实体类类名> list1;            //返回某一页的记录列表  
    3.     private List<实体类类名> list2;        //返回某一页的记录列表  
    4.     private int allRow;         //总记录数  
    5.     private int pageSize;       //每页记录数  
    6.     private int totalPage;      //总页数  
    7.     private int currentPage;    //当前页数  
    8.       
    9.     private boolean isFirstPage;        //是否为第一页  
    10.     private boolean isLastPage;         //是否为最后一页  
    11.     private boolean hasPreviousPage;    //是否有上一页  
    12.     private boolean hasNextPage;        //是否有下一页  
    13.       
    14.       
    15.     /** 
    16.      * 初始化分页信息 
    17.      */  
    18.     public void init(){  
    19.         this.isFirstPage=isFirstPage();  
    20.         this.isLastPage=isLastPage();  
    21.         this.hasPreviousPage=isHasPreviousPage();  
    22.         this.hasNextPage=isHasNextPage();  
    23.     }  
    24.       
    25.     /** 
    26.      * 判断分页信息,只需getter方法 
    27.      */  
    28.     public boolean isFirstPage(){  
    29.         return currentPage==1;  
    30.     }  
    31.       
    32.     public boolean isLastPage(){  
    33.         return currentPage==totalPage;  
    34.     }  
    35.       
    36.     public boolean isHasPreviousPage(){  
    37.         return currentPage!=1;  
    38.     }  
    39.       
    40.     public boolean isHasNextPage(){  
    41.         return currentPage!=totalPage;  
    42.     }  
    43.       
    44.       
    45.         /**  
    46.        * 计算总页数,静态方法,供外部直接通过类名调用  
    47.        * @param pageSize 每页记录数  
    48.        * @param allRow 总记录数  
    49.        * @return 总页数  
    50.        */   
    51.     public static int countTotalPage(final int pageSize,final int allRow){   
    52.         int totalPage = allRow % pageSize == 0 ? allRow/pageSize : allRow/pageSize+1;   
    53.         return totalPage;  
    54.     }  
    55.   
    56.        /** 
    57.       * 计算当前页开始记录 
    58.       * @parampageSize每页记录数 
    59.       * @paramcurrentPage当前第几页 
    60.       * @return当前页开始记录号 
    61.       */  
    62.     public static int countOffset(final int pageSize,final int currentPage){   
    63.         final int offset = pageSize*(currentPage-1);   
    64.         return offset;   
    65.     }   
    66.   
    67.         /**   
    68.        * 计算当前页,若为0或者请求的URL中没有"?page=",则用1代替  
    69.        * @param page 传入的参数(可能为空,即0,则返回1)  
    70.        * @return 当前页  
    71.        */   
    72.     public static int countCurrentPage(int page){   
    73.         final int curPage = (page==0?1:page);   
    74.         return curPage;   
    75.     }   
    76.       
    77.       
    78.     public List<实体类类名> getList1() {  
    79.         return list1;  
    80.     }  
    81.   
    82.     public void setList1(List<实体类类名> list1) {  
    83.         this.list1 = list1;  
    84.     }  
    85.           
    86.       
    87.     public List<实体类类名> getList2() {  
    88.         return list2;  
    89.     }  
    90.   
    91.     public void setList2(List<实体类类名> list2) {  
    92.         this.list2 = list2;  
    93.     }  
    94.   
    95.     public int getAllRow() {  
    96.         return allRow;  
    97.     }  
    98.     public void setAllRow(int allRow) {  
    99.         this.allRow = allRow;  
    100.     }  
    101.     public int getPageSize() {  
    102.         return pageSize;  
    103.     }  
    104.     public void setPageSize(int pageSize) {  
    105.         this.pageSize = pageSize;  
    106.     }  
    107.     public int getTotalPage() {  
    108.         return totalPage;  
    109.     }  
    110.     public void setTotalPage(int totalPage) {  
    111.         this.totalPage = totalPage;  
    112.     }  
    113.     public int getCurrentPage() {  
    114.         return currentPage;  
    115.     }  
    116.     public void setCurrentPage(int currentPage) {  
    117.         this.currentPage = currentPage;  
    118.     }  
    119.       
    120.       
    121. }  

    pageNo(跳转时用到list属性封装类)

    1. public class PageNo {  
    2.     private int pageNumber;     //跳转到第几页  
    3.   
    4.     public int getPageNumber() {  
    5.         return pageNumber;  
    6.     }  
    7.   
    8.     public void setPageNumber(int pageNumber) {  
    9.         this.pageNumber = pageNumber;  
    10.     }  
    11.   
    12. }  
    2.action

    1. @SuppressWarnings("serial")  
    2. public class XxxAction extends ActionSupport implements Serializable {  
    3.     protected HttpSession getSession() {  
    4.         return ServletActionContext.getRequest().getSession();  
    5.     }  
    6.   
    7.     private XxxService xxxService;  
    8.       
    9.     public XxxService getXxxService() {  
    10.         return xxxService;  
    11.     }  
    12.   
    13.     public void setXxxService(XxxService xxxService) {  
    14.         this.xxxService = xxxService;  
    15.     }  
    16.       
    17.     private int page=1;     //第几页  
    18.     private PageBean pageBean;  
    19.       
    20.     public int getPage() {  
    21.         return page;  
    22.     }  
    23.   
    24.     public void setPage(int page) {  
    25.         this.page = page;  
    26.     }  
    27.       
    28.     public PageBean getPageBean() {  
    29.         return pageBean;  
    30.     }  
    31.   
    32.     public void setPageBean(PageBean pageBean) {  
    33.         this.pageBean = pageBean;  
    34.     }  
    35.   
    36.     private List<PageNo> numberList;  
    37.     private PageNo pageNo;  
    38.       
    39.     public PageNo getPageNo() {  
    40.         return pageNo;  
    41.     }  
    42.   
    43.     public void setPageNo(PageNo pageNo) {  
    44.         this.pageNo = pageNo;  
    45.     }  
    46.       
    47.     /** 
    48.      * 列表并分页 
    49.      * @return 
    50.      */  
    51.     public String getListAndPage(){  
    52.         pageBean=xxxService.queryForPage(this,10,page);  
    53.           
    54.         numberList=new ArrayList<PageNo>();  
    55.         if(pageBean.getTotalPage()>0){  
    56.             for(int i=0;i<pageBean.getTotalPage();i++){  
    57.                 pageNo=new PageNo();  
    58.                 pageNo.setPageNumber(i+1);  
    59.                 numberList.add(i, pageNo);  
    60.             }  
    61.         }  
    62.         getSession().setAttribute("numberList", numberList);  
    63.         return "success";  
    64.     }  
    65.       
    66. }  
    3.service,serviceImpl,dao略

    4.daoImpl

    1. public class XxxDaoImpl extends BaseDAO implements XxxDao {  
    2.         
    3.     /** 
    4.      * 列表并分页 
    5.      */  
    6.     @SuppressWarnings("unchecked")  
    7.     public PageBean queryForPage(XxxAction version,int pageSize,int page) {  
    8.         Session session=getSession();  
    9.         try{  
    10.             String sql=" from Entity as entity where 1=1 ";  
    11.             Query query1=session.createQuery(sql);  
    12.             Query query2=session.createQuery(sql);  
    13.               
    14.             List list1 = query1.list();  
    15.               
    16.             int allRow=list1.size();                                        //总记录数  
    17.             int totalPage=PageBean.countTotalPage(pageSize, allRow);    //总页数  
    18.             final int length=pageSize;                                  //每页记录数  
    19.             final int currentPage=PageBean.countCurrentPage(page);      //当前页,不要为0或url为空  
    20.               
    21.             final int offset=PageBean.countOffset(pageSize, page);  
    22.               
    23.             query2.setFirstResult(offset);   
    24.             query2.setMaxResults(length);   
    25.             List<PaiPubVersion> list2=query2.list();  
    26.               
    27.             //把分页信息保存到Bean中   
    28.             PageBean pageBean = new PageBean();   
    29.             pageBean.setPageSize(pageSize);  
    30.             pageBean.setCurrentPage(currentPage);   
    31.             pageBean.setAllRow(allRow);   
    32.             pageBean.setTotalPage(totalPage);   
    33.             pageBean.setList2(list2);   
    34.             pageBean.init();   
    35.               
    36.             return pageBean;  
    37.             }catch(RuntimeException re){  
    38.                 throw re;  
    39.             }finally{  
    40.                 if(session!=null){  
    41.                     session.close();  
    42.                 }  
    43.             }  
    44.     }  
    45.       
    46. }  
    5.jsp页面

    1. <td colspan="6">  
    2.             共<s:property value="pageBean.allRow"/> 条记录  
    3.             共<s:property value="pageBean.totalPage"/> 页   
    4.             当前第<s:property value="pageBean.currentPage"/>页  
    5.             <s:if test="%{pageBean.currentPage == 1}">  
    6.             <input type="button" value="第一页" disabled="disabled" />  
    7.             <input type="button" value="上一页" disabled="disabled" />  
    8.             </s:if>  
    9.             <s:else>  
    10.             <input type="button" value="第一页" onclick="pageNo('1')"/>  
    11.             <input type="button" value="上一页" onclick="pageNo('<s:property value="%{pageBean.currentPage-1}"/>')"/>  
    12.             </s:else>  
    13.             <s:if test="%{pageBean.currentPage != pageBean.totalPage}">  
    14.             <input type="button" value="下一页" onclick="pageNo('<s:property value="%{pageBean.currentPage+1}"/>')"/>  
    15.             <input type="button" value="最后一页" onclick="pageNo('<s:property value="%{pageBean.totalPage}"/>')"/>  
    16.             </s:if>  
    17.             <s:else>  
    18.             <input type="button" value="下一页" disabled="disabled" />  
    19.             <input type="button" value="最后一页" disabled="disabled" />  
    20.             </s:else>  
    21.             跳转到:  
    22.             <select id="gotoNo" name="gotoNo" onchange="onTiao()">  
    23.                 <s:iterator value="#session.numberList">  
    24.                     <s:if test="pageBean.currentPage eq pageNumber">  
    25.                     <option value="<s:property value="pageNumber"/>" selected="selected" ><s:property value="pageNumber"/></option>  
    26.                     </s:if>  
    27.                     <s:else>  
    28.                     <option value="<s:property value="pageNumber"/>"><s:property value="pageNumber"/></option>  
    29.                     </s:else>  
    30.                 </s:iterator>  
    31.             </select>  
    32.             页  
    33.         </td>  
    6.js

    1. function pageNo(fenye){  
    2.         document.pageform.action="getListAndPage.action?page="+fenye;  
    3.         document.pageform.submit();  
    4.     }  
    5.       
    6.     function onTiao(){  
    7.         var gotoNo = document.getElementById('gotoNo').value;  
    8.         document.pageform.action="getListAndPage.action?page="+gotoNo;  
    9.         document.pageform.submit();  
    10.     }  


    7.页面显示效果
  • 相关阅读:
    Openlayers 3 热力图
    javaScript 新学习:Array.contains 函数
    将页面内容转为Excel下载
    Cookie 的设置和获取
    escape()、encodeURI()、encodeURIComponent()区别详解
    java 对象与二进制互转
    获取与当前类同级目录下的文件
    Windows下比较小巧的c/c++ ide
    保存到properties
    javafx 普通弹框提示
  • 原文地址:https://www.cnblogs.com/archermeng/p/7537454.html
Copyright © 2011-2022 走看看