先来说一些题外话,为了实现这种方式的分页,公司里大约5-6个人做了半个多月的工作,扩展了dataTable,修改了dataScrollor,以及各种其他的方法,但是都不是很优雅。在上个月底的时候,在Myfaces的Mail List中也针对这个问题展开了一系列的讨论,最后有人总结了讨论中提出的比较好的方法,提出了以下的分页方法,也是目前实现的最为优雅的方法,也就是不对dataTable和dataScrollor做任何修改,仅仅通过扩展DataModel来实现分页。<o:p></o:p>
DataModel 是一个抽象类,用于封装各种类型的数据源和数据对象的访问,JSF中dataTable中绑定的数据实际上被包装成了一个DataModel,以消除各种不同数据源和数据类型的复杂性,在前面一篇中我们访问数据库并拿到了一个List,交给dataTable,这时候,JSF会将这个List包装成 ListDataModel ,dataTable访问数据都是通过这个DataModel进行的,而不是直接使用List。<o:p></o:p>
接下来我们要将需要的页的数据封装到一个DataPage中去,这个类表示了我们需要的一页的数据,里面包含有三个元素:datasetSize,startRow,和一个用于表示具体数据的List。datasetSize表示了这个记录集的总条数,查询数据的时候,使用同样的条件取count即可,startRow表示该页的起始行在数据库中所有记录集中的位置。
<o:p></o:p>
* 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仅仅持有了一页的数据DataPage,并在适当的时候加载数据,读取我们需要页的数据。<o:p></o:p>
* 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 = 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 objects to render in the table.
*/
public void setRowIndex( int index)
{
rowIndex = index;
}
/**
* Return the total number of rows of data available (not just the number of
* rows in the current page!).
*/
public int getRowCount()
{
return getPage().getDatasetSize();
}
/**
* Return a DataPage object; if one is not currently available then fetch
* one. Note that this doesn't ensure that the datapage returned includes
* the current rowIndex row; see getRowData.
*/
private DataPage getPage()
{
if (page != null )
{
return page;
}
int rowIndex = getRowIndex();
int startRow = rowIndex;
if (rowIndex == - 1 )
相关推荐
jsf 分页jsf 分页 可直接在myeclipse6.0以上版本运行
用jsf实现了分页,是将上一个jsp系统进行改造而成。有建表的sql语句,还用到了java反射。整个结构耦合度低,代码清晰易懂!
JSF分页有不少的组件可以使用,但用起来都不是想象的那么简单,这里自己实现了一个简单的分页实现,以提供大家分享。 有更好的意见请留言。或者去...
核心JSF编程提出了一个自定义渲染器实现分页显示大量数据的技术,对该书的代码进行改进和增强后可以得到一个功能强大、使用简单的分页标签。 本资源包含了该分页标签全部的修改后的代码、配置文件示例、使用方法示例...
针对使用JSF构建的系统中经常遇到的大量的分页查询问题。结合JSF自定义呈现器组件技术实现分页查询呈现 器,从而迭到显示逻辑和业务逻辑的分离、代码重用以及易于维护的目的。
JSF数据分页的简单实现,有兴趣的可以下来来自己参考一下。 有问题留言。
JSF2.xdatatable分页控件与左侧菜单最简单应用 JSF2.x,功能强大,使用方便。全世界使用JSF的人越来越多。而且也有很多很好的控件给发出来了,如:richfaces、primefaces、myfaces等,就日前来讲primefaces功能...
至此,分页已经实现。下面是使用截图: 二、左侧菜单: 1. xhtml文件: [html] view plaincopy <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...
本人初学jsf,做了一个jsf的增删改查以及分页,供大家参考
最近学习JSF,但资料较少,国内学习的人不多,自己摸索,采用JSF2.2、Hibernate5.0实现的一个JSF例子,简单实现了增删改查,尚未实现分页
用jsf+ jdbc+spring 写的增删改查,包括分页、导入数据、和导出数据
本分页程序是在myeclipse6.0.1下开发,物超所值绝对是你在网上找不到的很好的例子。myfaces1.1.5+hibernate3.1+spring实现大数据分页,并包括添加、修改和删除功能,添加修改和删除后均返回当前页面。数据库用的是...
完成于2008年12月,主要用JSF实现增删改查,带分页~以及各种组件标签的应用,包括复选框等.导航的应用.数据库用SQL2005. 注意:改一下连接端口,默认的是1433.
第二部分详细讲解了jsf ri、jta、jndi、rmi、jms、javamail、ejb 3的session bean、message driven bean、jpa、jax-ws 2、jaas等java ee知识,这部分知识以jsf+ejb 3+jpa整合开发为重点,通过使用netbeans ide工具...
使用ajax4jsf实现的datatable和分页 博文链接:https://johnyi.iteye.com/blog/119186
3、Dao -Data Access Object,以显示、增加、删除、修改(密码)为例,统一在userdao.java里,调用不同方法实现 pagedao,分页处理类 4、数据库 page.mdb,内含一个表user,含2个字段,uname,upassword
该项目的目标: 他制作了一个像Google这样的本地网站,并在java上进行了试用,它接收并传输了应用程序和歌曲文件。 项目的工作逻辑: 它是根据授权顺序上传到站点的文件列表,并向用户显示。 -有关项目和数据库中...
jsf 的简单例子,实现增删改查及分页,通过学习,很好的理解jsf开发。