JSF 中处理分页问题

WorkingWithLargeTables

本文转自http://wiki.apache.org/myfaces/WorkingWithLargeTables,由于该文最后一部分没有jsp代码部分,所有又加上了一些jsp代码。

Components t:dataModel and t:dataScroller work together nicely to allow a user to "page" through a set of a few dozen to a few hundred records. However the implementation does assume that the entire set of available records are in memory and wrapped up inside a ListDataModel or ArrayDataModel.

When the available dataset is quite large, and the application can have many users, this can lead to memory usage problems.

This page contains discussions on how to handle this scenario.

On-demand loading

A custom DataModel can be used to allow data to be loaded "on demand".

First, a class needs to be defined which your "business methods" (eg EJBs) can use to pass "pages" of data back to the UI. This class needs to be defined in your project, as the "business" level shouldn't be extending MyFaces classes:

  1. package example;   
  2.   
  3. import java.util.List;   
  4.   
  5. /**  
  6.  * A simple class that represents a "page" of data out of a longer set, ie  
  7.  * a list of objects together with info to indicate the starting row and  
  8.  * the full size of the dataset. EJBs can return instances of this type  
  9.  * when returning subsets of available data.  
  10.  */  
  11. public class DataPage<t></t> {   
  12.   
  13.   private int datasetSize;   
  14.   private int startRow;   
  15.   private List<t></t> data;   
  16.            
  17.   /**  
  18.    * Create an object representing a sublist of a dataset.  
  19.    *   
  20.    * @param datasetSize is the total number of matching rows  
  21.    * available.  
  22.    *   
  23.    * @param startRow is the index within the complete dataset  
  24.    * of the first element in the data list.  
  25.    *   
  26.    * @param data is a list of consecutive objects from the  
  27.    * dataset.  
  28.    */  
  29.   public DataPage(int datasetSize, int startRow, List<t></t> data) {   
  30.     this.datasetSize = datasetSize;   
  31.     this.startRow = startRow;   
  32.     this.data = data;   
  33.   }   
  34.   
  35.   /**  
  36.    * Return the number of items in the full dataset.  
  37.    */  
  38.   public int getDatasetSize() {   
  39.     return datasetSize;   
  40.   }   
  41.   
  42.   /**  
  43.    * Return the offset within the full dataset of the first  
  44.    * element in the list held by this object.  
  45.    */  
  46.   public int getStartRow() {   
  47.     return startRow;   
  48.   }   
  49.   
  50.   /**  
  51.    * Return the list of objects held by this object, which  
  52.    * is a continuous subset of the full dataset.  
  53.    */  
  54.   public List<t></t> getData() {   
  55.     return data;   
  56.   }   
  57. }   

Now a custom DataModel can use this DataPage stuff. Again, it is recommended that you copy this code into your project and change the package name appropriately. This class can't go in the MyFaces libraries as it depends on DataPage, and as noted above, DataPage is accessed by your business level code so it really can't be in the MyFaces libs:

  1. package example;   
  2.   
  3. import javax.faces.model.DataModel;   
  4.   
  5. import example.DataPage;   
  6.   
  7. /**  
  8.  * A special type of JSF DataModel to allow a datatable and datascroller  
  9.  * to page through a large set of data without having to hold the entire  
  10.  * set of data in memory at once.  
  11.  * 

     

     
  12.  * Any time a managed bean wants to avoid holding an entire dataset,  
  13.  * the managed bean should declare an inner class which extends this  
  14.  * class and implements the fetchData method. This method is called  
  15.  * as needed when the table requires data that isn't available in the  
  16.  * current data page held by this object.  
  17.  * 

     

     
  18.  * This does require the managed bean (and in general the business  
  19.  * method that the managed bean uses) to provide the data wrapped in  
  20.  * a DataPage object that provides info on the full size of the dataset.  
  21.  */  
  22. public abstract class PagedListDataModel<t></t> extends DataModel {   
  23.   
  24.     int pageSize;   
  25.     int rowIndex;   
  26.     DataPage<t></t> page;   
  27.        
  28.     /*  
  29.      * Create a datamodel that pages through the data showing the specified  
  30.      * number of rows on each page.  
  31.      */  
  32.     public PagedListDataModel(int pageSize) {   
  33.         super();   
  34.         this.pageSize = pageSize;   
  35.         this.rowIndex = -1;   
  36.         this.page = null;   
  37.     }   
  38.   
  39.     /**  
  40.      * Not used in this class; data is fetched via a callback to the  
  41.      * fetchData method rather than by explicitly assigning a list.  
  42.      */  
  43.     @Override  
  44.     public void setWrappedData(Object o) {   
  45.         throw new UnsupportedOperationException("setWrappedData");   
  46.     }   
  47.   
  48.     @Override  
  49.     public int getRowIndex() {   
  50.         return rowIndex;   
  51.     }   
  52.   
  53.     /**  
  54.      * Specify what the "current row" within the dataset is. Note that  
  55.      * the UIData component will repeatedly call this method followed  
  56.      * by getRowData to obtain the objects to render in the table.  
  57.      */  
  58.     @Override  
  59.     public void setRowIndex(int index) {   
  60.         rowIndex = index;   
  61.     }   
  62.   
  63.     /**  
  64.      * Return the total number of rows of data available (not just the  
  65.      * number of rows in the current page!).  
  66.      */  
  67.     @Override  
  68.     public int getRowCount() {   
  69.         return getPage().getDatasetSize();   
  70.     }   
  71.        
  72.     /**  
  73.      * Return a DataPage object; if one is not currently available then  
  74.      * fetch one. Note that this doesn't ensure that the datapage  
  75.      * returned includes the current rowIndex row; see getRowData.  
  76.      */  
  77.     private DataPage<t></t> getPage() {   
  78.         if (page != null)   
  79.             return page;   
  80.            
  81.         int rowIndex = getRowIndex();   
  82.         int startRow = rowIndex;   
  83.         if (rowIndex == -1) {   
  84.             // even when no row is selected, we still need a page   
  85.             // object so that we know the amount of data available.   
  86.            startRow = 0;   
  87.         }   
  88.   
  89.         // invoke method on enclosing class   
  90.         page = fetchPage(startRow, pageSize);   
  91.         return page;   
  92.     }   
  93.   
  94.     /**  
  95.      * Return the object corresponding to the current rowIndex.  
  96.      * If the DataPage object currently cached doesn't include that  
  97.      * index then fetchPage is called to retrieve the appropriate page.  
  98.      */  
  99.     @Override  
  100.     public Object getRowData(){   
  101.         if (rowIndex < 0) {   
  102.             throw new IllegalArgumentException(   
  103.                 "Invalid rowIndex for PagedListDataModel; not within page");   
  104.         }   
  105.   
  106.         // ensure page exists; if rowIndex is beyond dataset size, then    
  107.         // we should still get back a DataPage object with the dataset size   
  108.         // in it...   
  109.         if (page == null) {   
  110.             page = fetchPage(rowIndex, pageSize);   
  111.         }   
  112.   
  113.         // Check if rowIndex is equal to startRow,   
  114.         // useful for dynamic sorting on pages   
  115.                    
  116.         if (rowIndex == page.getStartRow()){   
  117.                 page = fetchPage(rowIndex, pageSize);   
  118.         }   
  119.   
  120.         int datasetSize = page.getDatasetSize();   
  121.         int startRow = page.getStartRow();   
  122.         int nRows = page.getData().size();   
  123.         int endRow = startRow + nRows;   
  124.            
  125.         if (rowIndex >= datasetSize) {   
  126.             throw new IllegalArgumentException("Invalid rowIndex");   
  127.         }   
  128.            
  129.         if (rowIndex < startRow) {   
  130.             page = fetchPage(rowIndex, pageSize);   
  131.             startRow = page.getStartRow();   
  132.         } else if (rowIndex >= endRow) {   
  133.             page = fetchPage(rowIndex, pageSize);   
  134.             startRow = page.getStartRow();   
  135.         }   
  136.            
  137.         return page.getData().get(rowIndex - startRow);   
  138.     }   
  139.   
  140.     @Override  
  141.     public Object getWrappedData() {   
  142.         return page.getData();   
  143.     }   
  144.   
  145.     /**  
  146.      * Return true if the rowIndex value is currently set to a  
  147.      * value that matches some element in the dataset. Note that  
  148.      * it may match a row that is not in the currently cached   
  149.      * DataPage; if so then when getRowData is called the  
  150.      * required DataPage will be fetched by calling fetchData.  
  151.      */  
  152.     @Override  
  153.     public boolean isRowAvailable() {   
  154.         DataPage<t></t> page = getPage();   
  155.         if (page == null)   
  156.             return false;   
  157.            
  158.         int rowIndex = getRowIndex();   
  159.         if (rowIndex < 0) {   
  160.             return false;   
  161.         } else if (rowIndex >= page.getDatasetSize()) {   
  162.             return false;   
  163.         } else {   
  164.             return true;   
  165.         }   
  166.     }   
  167.   
  168.     /**  
  169.      * Method which must be implemented in cooperation with the  
  170.      * managed bean class to fetch data on demand.  
  171.      */  
  172.     public abstract DataPage<t></t> fetchPage(int startRow, int pageSize);   
  173. }   

Finally, the managed bean needs to provide a simple inner class that provides the fetchPage implementation:

java 代码
  1. public SomeManagedBean {   
  2.    ....   
  3.   
  4.   
  5.    private DataPage<somerowobject></somerowobject> getDataPage(int startRow, int pageSize) {   
  6.      // access database here, or call EJB to do so   
  7.    }   
  8.   
  9.    public DataModel getDataModel() {   
  10.        if (dataModel == null) {   
  11.            dataModel = new LocalDataModel(getRowsPerPage());   
  12.        }   
  13.   
  14.        return dataModel;   
  15.    }   
  16.   
  17.    private class LocalDataModel extends PagedListDataModel {   
  18.        public LocalDataModel(int pageSize) {   
  19.            super(pageSize);   
  20.        }   
  21.           
  22.        public DataPage<somerowobject></somerowobject> fetchPage(int startRow, int pageSize) {   
  23.            // call enclosing managed bean method to fetch the data   
  24.            return getDataPage(startRow, pageSize);   
  25.        }   
  26.    }   

The jsp pages should be like this:

     

  1.   
  2. public class PagedTableListBean {   
  3.   
  4.     private DataModel pagedDataModel;   
  5.   
  6.     private TableListBean listBean;   
  7.   
  8.     /**  
  9.      *   
  10.      */  
  11.     public PagedTableListBean() {   
  12.   
  13.     }   
  14.   
  15.     /**  
  16.      * @return the itemList  
  17.      */  
  18.     public DataModel getItemList() {   
  19.         if (pagedDataModel == null) {   
  20.             pagedDataModel = new LocalDataModel(15);   
  21.         }   
  22.   
  23.         return pagedDataModel;   
  24.     }   
  25.   
  26.     /**  
  27.      * @return the pagedDataModel  
  28.      */  
  29.     public DataModel getPagedDataModel() {   
  30.         return pagedDataModel;   
  31.     }   
  32.   
  33.     /**  
  34.      * @param pagedDataModel  
  35.      *            the pagedDataModel to set  
  36.      */  
  37.     public void setPagedDataModel(DataModel pagedDataModel) {   
  38.         this.pagedDataModel = pagedDataModel;   
  39.     }   
  40.   
  41.     /**  
  42.      * @return the listBean  
  43.      */  
  44.     public TableListBean getListBean() {   
  45.         return listBean;   
  46.     }   
  47.   
  48.     /**  
  49.      * @param listBean  
  50.      *            the listBean to set  
  51.      */  
  52.     public void setListBean(TableListBean listBean) {   
  53.         this.listBean = listBean;   
  54.     }   
  55.   
  56.     // local class   
  57.     private class LocalDataModel extends PagedListDataModel {   
  58.         public LocalDataModel(int pageSize) {   
  59.             super(pageSize);   
  60.         }   
  61.   
  62.         public DataPage fetchPage(int startRow, int pageSize) {   
  63.             // call enclosing managed bean method to fetch the data   
  64.             return getDataPage(startRow, pageSize);   
  65.         }   
  66.     }   
  67.   
  68.     // get datePage from DAO   
  69.     private DataPage getDataPage(int startRow, int pageSize) {   
  70.         // access database here, or call EJB to do so   
  71.        
  72.         List subList = listBean.getTableList(startRow, pageSize);   
  73.         DataPage dataPage = new DataPage(listBean.getTotal(), startRow, subList);   
  74.         return dataPage;   
  75.     }   
  76.   
  77. }   

页面的绑定:

  1. <hx:dataTableEx id="tableEx1" value="#{pagedTableListBean.itemList}"  
  2.                 var="vartableList" styleClass="dataTableEx" headerClass="headerClass"  
  3.                 footerClass="footerClass" rowClasses="rowClass1, rowClass2"  
  4.                 columnClasses="columnClass1" border="1" cellpadding="2"  
  5.                 cellspacing="0" rows="15">  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值