|
前面一篇直接使用了Myfaces中的兩個Component完成了一個簡單的分頁,這里將會介紹一種On-demand loading的方法來進行分頁,僅僅在需要數(shù)據(jù)的時候加載。 先來說一些題外話,為了實現(xiàn)這種方式的分頁,公司里大約5-6個人做了半個多月的工作,擴展了dataTable,修改了dataScrollor,以及各種其他的方法,但是都不是很優(yōu)雅。在上個月底的時候,在Myfaces的Mail List中也針對這個問題展開了一系列的討論,最后有人總結(jié)了討論中提出的比較好的方法,提出了以下的分頁方法,也是目前實現(xiàn)的最為優(yōu)雅的方法,也就是不對dataTable和dataScrollor做任何修改,僅僅通過擴展DataModel來實現(xiàn)分頁。 DataModel 是一個抽象類,用于封裝各種類型的數(shù)據(jù)源和數(shù)據(jù)對象的訪問,JSF中dataTable中綁定的數(shù)據(jù)實際上被包裝成了一個DataModel,以消除各種不同數(shù)據(jù)源和數(shù)據(jù)類型的復雜性,在前面一篇中我們訪問數(shù)據(jù)庫并拿到了一個List,交給dataTable,這時候,JSF會將這個List包裝成 ListDataModel ,dataTable訪問數(shù)據(jù)都是通過這個DataModel進行的,而不是直接使用List。 接下來我們要將需要的頁的數(shù)據(jù)封裝到一個DataPage中去,這個類表示了我們需要的一頁的數(shù)據(jù),里面包含有三個元素:datasetSize,startRow,和一個用于表示具體數(shù)據(jù)的List。datasetSize表示了這個記錄集的總條數(shù),查詢數(shù)據(jù)的時候,使用同樣的條件取count即可,startRow表示該頁的起始行在數(shù)據(jù)庫中所有記錄集中的位置。 /** */ /** * A simple class that represents a "page" of data out of a longer set, ie a * list of objects together with info to indicate the starting row and the full * size of the dataset. EJBs can return instances of this type when returning * subsets of available data. */ public class DataPage![]() { private int datasetSize; private int startRow; private List data;![]() ![]() /** */ /** * Create an object representing a sublist of a dataset. * * @param datasetSize * is the total number of matching rows available. * * @param startRow * is the index within the complete dataset of the first element * in the data list. * * @param data * is a list of consecutive objects from the dataset. */ public DataPage( int datasetSize, int startRow, List data)![]() { this .datasetSize = datasetSize; this .startRow = startRow; this .data = data; } /** */ /** * Return the number of items in the full dataset. */ public int getDatasetSize()![]() { return datasetSize; } /** */ /** * Return the offset within the full dataset of the first element in the * list held by this object. */ public int getStartRow()![]() { return startRow; } /** */ /** * Return the list of objects held by this object, which is a continuous * subset of the full dataset. */ public List getData()![]() { return data; } } 接下來,我們要對DataModel進行封裝,達到我們分頁的要求。該DataModel僅僅持有了一頁的數(shù)據(jù)DataPage,并在適當?shù)臅r候加載數(shù)據(jù),讀取我們需要頁的數(shù)據(jù)。 /** */ /** * A special type of JSF DataModel to allow a datatable and datascroller to page * through a large set of data without having to hold the entire set of data in * memory at once. * <p> * Any time a managed bean wants to avoid holding an entire dataset, the managed * bean should declare an inner class which extends this class and implements * the fetchData method. This method is called as needed when the table requires * data that isn‘t available in the current data page held by this object. * <p> * This does require the managed bean (and in general the business method that * the managed bean uses) to provide the data wrapped in a DataPage object that * provides info on the full size of the dataset. */ public abstract class PagedListDataModel extends DataModel![]() { int pageSize; int rowIndex; DataPage page;![]() ![]() /** */ /** * Create a datamodel that pages through the data showing the specified * number of rows on each page. */ public PagedListDataModel( int pageSize)![]() { super (); this .pageSize =
本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。 |