在Web应用中如果遇到大数据集要处理时,分页显示往往是一个很好的解决方法。但是在JSF中没有直接实现分页显示的组件。但在Myfaces里的分页组件dataScroller如果直接显示的话,必须一次性的比所有要显示的记录都要检索出来,如果有10000条记录的话,一次性把这些记录全部检索出来的话,那么所消耗的内存是不敢想像的。所以我们应该做的是需要一页就检索一页。我最近在
http://wiki.apache.org/myfaces/WorkingWithLargeTables上页看到的一篇文章就实现了我所实现的功能。下面是我应用此方法所实现的一个分页显示:
首先得写一个用来包装一个页面数据的类:
package doorban.business;
import java.util.List;
public class DataPage {
private int datasetSize; //数据集的记录总数
private int startRow; //从哪一条记录开始取数据
private List data; //一个页面的记录集
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的类
package doorban.business;
import javax.faces.model.DataModel;
/**
* 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.
* 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.
* 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) {
// even when no row is selected, we still need a page
// object so that we know the amount of data available.
startRow = 0;
}
// invoke method on enclosing class
page = fetchPage(startRow, pageSize);
return page;
}
/**
* Return the object corresponding to the current rowIndex. If the DataPage
* object currently cached doesn\'t include that index then fetchPage is
* called to retrieve the appropriate page.
*/
public Object getRowData() {
if (rowIndex < 0) {
throw new IllegalArgumentException(
"Invalid rowIndex for PagedListDataModel; not within page");
}
// ensure page exists; if rowIndex is beyond dataset size, then
// we should still get back a DataPage object with the dataset size
// in it if (page == null)
{
page = fetchPage(rowIndex, pageSize);
}
int datasetSize = page.getDatasetSize();
int startRow = page.getStartRow();
int nRows = page.getData().size();
int endRow = startRow + nRows;
if (rowIndex >= datasetSize) {
throw new IllegalArgumentException("Invalid rowIndex");
}
if (rowIndex < startRow) {
page = fetchPage(rowIndex, pageSize);
startRow = page.getStartRow();
} else if (rowIndex >= endRow) {
page = fetchPage(rowIndex, pageSize);
startRow = page.getStartRow();
}
return page.getData().get(rowIndex - startRow);
}
public Object getWrappedData() {
return page.getData();
}
/**
* Return true if the rowIndex value is currently set to a value that
* matches some element in the dataset. Note that it may match a row that is
* not in the currently cached DataPage; if so then when getRowData is
* called the required DataPage will be fetched by calling fetchData.
*/
public boolean isRowAvailable() {
DataPage page = getPage();
if (page == null) {
return false;
}
int rowIndex = getRowIndex();
if (rowIndex < 0) {
return false;
} else if (rowIndex >= page.getDatasetSize()) {
return false;
} else {
return true;
}
}
/**
* 这个方法在每一个业务里里必须要被实现
* startRow 每页的第一条记录在数据集里的位置
* pageSize 每页的记录数
*/
public abstract DataPage fetchPage(int startRow, int pageSize);
}
接着就应该来写一个具体的业务类
package doorban.business;
import java.util.ArrayList;
import java.util.List;
import javax.faces.model.DataModel;
import doorban.dao.hb.TempCardActionHB;
public class TCB {
private DataModel dataModel = null;
private int dataSetSize;
// dataSetSize = actionHB.getTotal();
TempCardActionHB actionHB = new TempCardActionHB();
public DataModel getDataModel() {
if (dataModel == null) {
dataModel = new LocalDataModel(5);
}
return dataModel;
}
private class LocalDataModel extends PagedListDataModel {
public LocalDataModel(int pageSize) {
super(pageSize);
try {
dataSetSize = actionHB.getTotal();
} catch (Exception e) {
e.printStackTrace();
}
}
public DataPage fetchPage(int startRow, int pageSize) {
TempCardActionHB actionHB = new TempCardActionHB();
List list = new ArrayList();
try {
list = actionHB.getTempCards(startRow, pageSize);
} catch (Exception e) {
e.printStackTrace();
}
DataPage dataPage = null;
try {
dataPage = new DataPage(dataSetSize, startRow, list);
} catch (Exception e) {
e.printStackTrace();
}
return dataPage;
}
}
}
分享到:
相关推荐
jsf 分页jsf 分页 可直接在myeclipse6.0以上版本运行
JSF分页有不少的组件可以使用,但用起来都不是想象的那么简单,这里自己实现了一个简单的分页实现,以提供大家分享。 有更好的意见请留言。或者去...
用jsf实现了分页,是将上一个jsp系统进行改造而成。有建表的sql语句,还用到了java反射。整个结构耦合度低,代码清晰易懂!
JSF数据分页的简单实现,有兴趣的可以下来来自己参考一下。 有问题留言。
核心JSF编程提出了一个自定义渲染器实现分页显示大量数据的技术,对该书...本资源包含了该分页标签全部的修改后的代码、配置文件示例、使用方法示例以及对代码的有关说明,掌握本资源的内容,从此JSF分页可以一劳永逸。
针对使用JSF构建的系统中经常遇到的大量的分页查询问题。结合JSF自定义呈现器组件技术实现分页查询呈现 器,从而迭到显示逻辑和业务逻辑的分离、代码重用以及易于维护的目的。
最近学习JSF,但资料较少,国内学习的人不多,自己摸索,采用JSF2.2、Hibernate5.0实现的一个JSF例子,简单实现了增删改查,尚未实现分页
本人初学jsf,做了一个jsf的增删改查以及分页,供大家参考
至此,分页已经实现。下面是使用截图: 二、左侧菜单: 1. xhtml文件: [html] view plaincopy <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...
JSF2.xdatatable分页控件与左侧菜单最简单应用 JSF2.x,功能强大,使用方便。全世界使用JSF的人越来越多。而且也有很多很好的控件给发出来了,如:richfaces、primefaces... 至此,分页已经实现。下面是使用截图:
用jsf+ jdbc+spring 写的增删改查,包括分页、导入数据、和导出数据
本分页程序是在myeclipse6.0.1下开发,物...myfaces1.1.5+hibernate3.1+spring实现大数据分页,并包括添加、修改和删除功能,添加修改和删除后均返回当前页面。数据库用的是mysql,上传时略去了jar包。sql.sql是sql语句
完成于2008年12月,主要用JSF实现增删改查,带分页~以及各种组件标签的应用,包括复选框等.导航的应用.数据库用SQL2005. 注意:改一下连接端口,默认的是1433.
这个项目包括5个实体,这5个实体之间具有复杂的关联关系,而且业务逻辑也相对复杂,希望让读者理论联系实际,真正将jsf+ejb 3+jpa整合真正运用到实际开发中。该案例采用目前最流行、最规范的java ee架构,整个应用...
使用ajax4jsf实现的datatable和分页 博文链接:https://johnyi.iteye.com/blog/119186
Jsf访问access(*.mdb)数据库增删查改案例 开发环境:Eclipse2018,Jdk1.8,Jre1.8,Tomcat9.0.17 项目简介:VCD模式 1、VO-值对象,user.java 2、Conn-数据库通用连接代码,dbconfig.properties(列示数据库位置) ...
2.6 jsf框架的实现 2.7 ajax框架的实现 2.8 使用jdbc连接数据库 2.9 小结 第3章 实现各种框架的集成 3.1 spring框架与其他框架的集成原理 3.2 实现ssh三种框架环境集成 3.3 实现spring与...
jsf 的简单例子,实现增删改查及分页,通过学习,很好的理解jsf开发。