在jsf中实现分页

上传人:第*** 文档编号:32829123 上传时间:2018-02-12 格式:DOCX 页数:19 大小:63.78KB
返回 下载 相关 举报
在jsf中实现分页_第1页
第1页 / 共19页
在jsf中实现分页_第2页
第2页 / 共19页
在jsf中实现分页_第3页
第3页 / 共19页
在jsf中实现分页_第4页
第4页 / 共19页
在jsf中实现分页_第5页
第5页 / 共19页
点击查看更多>>
资源描述

《在jsf中实现分页》由会员分享,可在线阅读,更多相关《在jsf中实现分页(19页珍藏版)》请在金锄头文库上搜索。

1、在 JSF 中实现分页(一) 对于大多数 Web 应用,分页都是必不可少的功能,当然在 JSF 中也一样,我在这里用两篇文章介绍两种方法来展示一下,如何在 JSF 中实现分页。本文假定你已经对 JSF 有了一些简单的了解,懂得基本配置和使用,并建立起一个 blank 项目。Myfaces 是 Apache 基金会中的一个一级项目,除了实现 JSF 标准外,做了很多的扩展工作,在 Myfaces 包中有一个扩展包 Tomahawk,我们将主要使用其中的两个 Component 实现分页:一个是,另一个是,在第一篇里面,我们简易的组合这两个 Component 来实现一种简单,但并不高效的分页。下

2、面的例子来自于 Myfaces-Sample,我省去了其中和分页逻辑无关的内容,详细的例子可以下载 Myfaces-Sample 包或者访问 http:/www.irian.at/myfaces/home.jsf 查看。第一部分:dataTable在这一部分中,dataTable 绑定了一个 backing bean - pagedSort 中的 cars 属性,我们可以在这个属性中加入数据访问逻辑,从数据库或者其他来源取得用于显示的数据。比如我们可以通过 Hibernate 获取一个 List,其中包含有我们用于显示的 POJOs。注意,dataTable 中的 rows 属性指的是每页的行

3、数,是必须指定的,否则是无法进行分页的,如果在项目中会使用固定行数的分页,建议把这个值写在 BaseBackingBean 中,并暴露一个 property,供页面调用,所以每次在页面中就可以这么写#backingBean.pageSize。第二部分:dataScroller 这里定义了我们用于分页的,最主要的是配置该分页 Component 针对哪个 dataTable 进行分页的“for”属性,该属性与 dataTable 绑定,并对其进行分页,在这里,绑定了第一部分中的id=data的 dataTable,下面有很多的是指定分页的导航样式的,这里使用了图片作为导航,可以把他们改成文字形式

4、的导航。当然这只是最简单,也是一种不推荐的分页方式,因为在每次进行分页的时候,将会从数据库中取回所有的记录放入 List 中,然后,dataScroller 在对这个 List 进行分页,如果在数据量很大的情况下,这种方式显然是不符合要求的,假设每条记录占用 1k 内存,数据库中有 100 万条记录,每次要把这个 List 全部读取出来将占用 1G 内存。我们需要一种 Load on demand 方式的读取,也就是只在需要查看某页的时候读取该页的数据。另外一方面,JSF 的生命周期中有多个阶段会调用到#pagedSort.cars中对应的方法,如果在这里调用了数据访问逻辑,就会在只显示一次页

5、面的情况下进行多次数据库操作,也是相当的耗费资源的。所以我们需要有更好的分页方式去解决以上问题,下一篇我将介绍另一种方法以改善这些问题。前面一篇直接使用了 Myfaces 中的两个 Component 完成了一个简单的分页,这里将会介绍一种 On-demand loading 的方法来进行分页,仅仅在需要数据的时候加载。 先来说一些题外话,为了实现这种方式的分页,公司里大约 5-6 个人做了半个多月的工作,扩展了 dataTable,修改了 dataScrollor,以及各种其他的方法,但是都不是很优雅。在上个月底的时候,在 Myfaces 的 Mail List 中也针对这个问题展开了一系列

6、的讨论,最后有人总结了讨论中提出的比较好的方法,提出了以下的分页方法,也是目前实现的最为优雅的方法,也就是不对 dataTable 和 dataScrollor 做任何修改,仅仅通过扩展 DataModel 来实现分页。 DataModel 是一个抽象类,用于封装各种类型的数据源和数据对象的访问,JSF 中 dataTable 中绑定的数据实际上被包装成了一个 DataModel,以消除各种不同数据源和数据类型的复杂性,在前面一篇中我们访问数据库并拿到了一个 List,交给 dataTable,这时候,JSF 会将这个 List 包装成 ListDataModel ,dataTable 访问数

7、据都是通过这个 DataModel 进行的,而不是直接使用 List。 接下来我们要将需要的页的数据封装到一个 DataPage 中去,这个类表示了我们需要的一页的数据,里面包含有三个元素:datasetSize,startRow,和一个用于表示具体数据的 List。datasetSize 表示了这个记录集的总条数,查询数据的时候,使用同样的条件取 count 即可,startRow 表示该页的起始行在数据库中所有记录集中的位置。 /* * A simple class that represents a page of data out of a longer set, ie a* list

8、 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 DataPageprivate int datasetSize;private int startRow;private List data;/* * Create an object representing a

9、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 st

10、artRow, 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.*

11、/ 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 仅仅持有了一页的数据 DataPage,并在适当的时候加载数据,读取我们需要页的数据。 /* * A special type of JSF

12、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.* * 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 imp

13、lements* the fetchData method. This method is called as needed when the table requires* data that isnt available in the current data page held by this object.* * This does require the managed bean (and in general the business method that* the managed bean uses) to provide the data wrapped in a DataP

14、age object that* provides info on the full size of the dataset.*/ public abstract class PagedListDataModel extends DataModelint pageSize;int rowIndex; DataPage page;/* * Create a datamodel that pages through the data showing the specified* number of rows on each page.*/ public PagedListDataModel( in

15、t pageSize)super ();this .pageSize = pageSize;this .rowIndex = - 1 ;this .page = null ; /* * Not used in this class; data is fetched via a callback to the fetchData* method rather than by explicitly assigning a list.*/ public void setWrappedData(Object o)if (o instanceof DataPage)this .page = (DataPage) o; else throw new UnsupportedOperationException( setWrappedData ); public int getRowIndex()return rowIndex; /* * Specify what the current row within the dataset is. Note that the* UIData component will repeatedly call this method followed by getRowData* to obtain the

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 工程造价

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号