转载

SSH2实现数据库和界面的分页

分页应该是在我们开发web应用时经常要做的工作,能够比较简洁的实现数据库和视图层的分页十分重要。

在数据库层利用Hibernate进行数据库的分页,将从数据库中查询出的数据封装为javabean;在视图层就可以方便的实现分页。

创建PageBean

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

在Dao的抽象接口BaseDao中添加方法

  1. public List queryForPage(final String hql, final int offset,  
  2.             final int length); 

在Dao的实现类JianSheDWDaoImpl中实现方法

  1. public List queryForPage(final String hql, final int offset,  
  2.             final int length) {  
  3.         List list = getHibernateTemplate().executeFind(new HibernateCallback() {  
  4.  
  5.             public Object doInHibernate(Session session)  
  6.                     throws HibernateException, SQLException {  
  7.                 Query query = session.createQuery(hql);  
  8.                 query.setFirstResult(offset);  
  9.                 query.setMaxResults(length);  
  10.                 List list = query.list();  
  11.                 return list;  
  12.  
  13.             }  
  14.         });  
  15.  
  16.         return list;  
  17.     } 

在service抽象层接口JianSheDWService中添加方法

  1. public PageBean queryForPage(int pageSize,int currentPage);  

在service实现类中实现方法

  1. public PageBean queryForPage(int pageSize, int page) {  
  2.         final String hql = "from JianSheDWBean"// 查询语句  
  3.         int allRow = this.baseDao.getAllRowCount(hql); // 总记录数  
  4.         int totalPage = PageBean.countTotalPage(pageSize, allRow); // 总页数  
  5.         final int offset = PageBean.countOffset(pageSize, page); // 当前页开始记录  
  6.         final int length = pageSize; // 每页记录数  
  7.         final int currentPage = PageBean.countCurrentPage(page);  
  8.         List<JianSheDWBean> list = this.baseDao.queryForPage(hql, offset, length); // "一页"的记录  
  9.  
  10.         // 把分页信息保存到Bean中  
  11.         PageBean pageBean = new PageBean();  
  12.         pageBean.setPageSize(pageSize);  
  13.         pageBean.setCurrentPage(currentPage);  
  14.         pageBean.setAllRow(allRow);  
  15.         pageBean.setTotalPage(totalPage);  
  16.         pageBean.setList(list);  
  17.         pageBean.init();  
  18.         return pageBean;  
  19.  
  20.     } 

在视图层action中建立分页模型

  1. package com.fishing.action.lcq;  
  2.  
  3.  
  4.  
  5. import com.fishing.common.bean.JianSheDWBean;  
  6. import com.fishing.common.bean.PageBean;  
  7. import com.fishing.service.lcq.JianSheDWService;  
  8. import com.opensymphony.xwork2.ActionSupport;  
  9.  
  10. @SuppressWarnings("serial")  
  11. public class GetInfoJSDWListAction extends ActionSupport {  
  12.  
  13.     private int page; // 第几页  
  14.  
  15.     private PageBean pageBean; // 包含分布信息的bean  
  16.  
  17.     private JianSheDWBean jianSheDWBean;  
  18.     // private PageBean page;  
  19.     private JianSheDWService jianSheDWService;  
  20.  
  21.     public int getPage() {  
  22.         return page;  
  23.     }  
  24.  
  25.     public void setPage(int page) {  
  26.         this.page = page;  
  27.     }  
  28.  
  29.     public PageBean getPageBean() {  
  30.         return pageBean;  
  31.     }  
  32.  
  33.     public void setPageBean(PageBean pageBean) {  
  34.         this.pageBean = pageBean;  
  35.     }  
  36.  
  37.     public JianSheDWBean getJianSheDWBean() {  
  38.         return jianSheDWBean;  
  39.     }  
  40.  
  41.     public void setJianSheDWBean(JianSheDWBean jianSheDWBean) {  
  42.         this.jianSheDWBean = jianSheDWBean;  
  43.     }  
  44.  
  45.     public JianSheDWService getJianSheDWService() {  
  46.         return jianSheDWService;  
  47.     }  
  48.  
  49.     public void setJianSheDWService(JianSheDWService jianSheDWService) {  
  50.         this.jianSheDWService = jianSheDWService;  
  51.     }  
  52.  
  53.     @Override     
  54.      public String execute() throws Exception {      
  55.           
  56.      //分页的pageBean,参数pageSize表示每页显示记录数,page为当前页      
  57.      this.pageBean = jianSheDWService.queryForPage(10, page);      
  58.      return SUCCESS;  
  59.     }  
  60. }  

在jsp中编写分页

  1. <tr class="odd"> 
  2.                                                     <td> 
  3.                                                         &nbsp;  
  4.                                                     </td> 
  5.                                                     <td> 
  6.                                                         <s:if test="%{pageBean.currentPage == 1}">     
  7.                                           首页      上一页  
  8.                                             </s:if> 
  9.                                                         <s:else> 
  10.                                                             <a href="jianguan/getJSDWInfos.action?page=1">首页</a> 
  11.                                                             <a 
  12.                                                                 href="jianguan/getJSDWInfos.action?page=<s:property value="%{pageBean.currentPage-1}"/>/>上一页</a> 
  13.  
  14.                                                         </s:else> 
  15.                                                     </td> 
  16.                                                     <td> 
  17.                                                         <s:if test="%{pageBean.currentPage != pageBean.totalPage}"> 
  18.  
  19.                                                             <a 
  20.                                                                 href="jianguan/getJSDWInfos.action?page=<s:property value="%{pageBean.currentPage+1}"/>">下一页</a> 
  21.                                                             <a 
  22.                                                                 href="jianguan/getJSDWInfos.action?page=<s:property value="pageBean.totalPage"/>">尾页  
  23.                                                             </a> 
  24.                                                         </s:if> 
  25.                                                         <s:else>   
  26.                                           下一页  尾页   
  27.                                             </s:else> 
  28.                                                     </td> 
  29.  
  30.                                                     <td> 
  31.                                                         <div align="center"> 
  32.                                                             页次  
  33.                                                             <s:property value="pageBean.currentPage" /> 
  34.                                                             /  
  35.                                                             <s:property value="pageBean.totalPage" /> 
  36.                                                             &nbsp;&nbsp;&nbsp;共  
  37.                                                             <s:property value="pageBean.allRow" /> 
  38.                                                             记录  
  39.                                                         </div> 
  40.                                                         <div align="center"></div> 
  41.                                                     </td> 
  42.                                                 </tr> 

上面只是代码的实现,没有说明配置文件的配置,读者根据情况配置。

正文到此结束
Loading...