Server side Pagination using JSF Data table and Hibernate

Following is the working example of Dynamic or server side pagination.

Your DataModel class

/**
*
*/

/**
* @author muhammad.shouab
*
*/
import java.util.List;
import javax.faces.model.DataModel;

public class PagedListDataModel extends DataModel {

private int rowIndex = -1;

private int totalNumRows;

private int pageSize;

@SuppressWarnings("unchecked")
private List list;

public PagedListDataModel() {
super();
}

@SuppressWarnings("unchecked")
public PagedListDataModel(List list, int totalNumRows, int pageSize) {
super();
setWrappedData(list);
this.totalNumRows = totalNumRows;
this.pageSize = pageSize;
}

public boolean isRowAvailable() {
if (list == null)
return false;

int rowIndex = getRowIndex();
if (rowIndex >= 0 && rowIndex < list.size())
return true;
else
return false;
}

public int getRowCount() {
return totalNumRows;
}

public Object getRowData() {
if (list == null)
return null;
else if (!isRowAvailable())
throw new IllegalArgumentException();
else {
int dataIndex = getRowIndex();
return list.get(dataIndex);
}
}

public int getRowIndex() {
return (rowIndex % pageSize);
}

public void setRowIndex(int rowIndex) {
this.rowIndex = rowIndex;
}

public Object getWrappedData() {
return list;
}

@SuppressWarnings("unchecked")
public void setWrappedData(Object list) {
this.list = (List) list;
}

}

JSF Component Util to find datatable at runtime


import java.util.Iterator;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

/**
* @author muhammad.shouab
*
*/
public class JSFComponentUtil {
public static UIComponent findComponentInRoot(String id) {
UIComponent component = null;

FacesContext facesContext = FacesContext.getCurrentInstance();
if (facesContext != null) {
UIComponent root = facesContext.getViewRoot();
component = findComponent(root, id);
}

return component;
}

public static UIComponent findComponent(UIComponent base, String id) {
if (id.equals(base.getId()))
return base;

UIComponent kid = null;
UIComponent result = null;
Iterator kids = base.getFacetsAndChildren();
while (kids.hasNext() && (result == null)) {
kid = (UIComponent) kids.next();
if (id.equals(kid.getId())) {
result = kid;
break;
}
result = findComponent(kid, id);
if (result != null) {
break;
}
}
return result;
}
}


Your Controller Method

public DataModel getPagedDataModel()
{

try
{
HtmlDataTable table = (HtmlDataTable) JSFComponentUtil.findComponentInRoot("yourDataTableID");
Long totalListSize = Your Manager Method which return data List size
List data = Your Manager Method which return data List
PagedListDataModel dataModel = null;
if (totalListSize != null && totalListSize > 0)
{
dataModel = new PagedListDataModel(data, totalListSize.intValue(), table.getRows());
}
return dataModel;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}

Your JSF Page

Advertisements

About shouab

I am Shouab working as Pr Software Engg. I have 6 plus years of experience in Enterprise Application Development. I have expertise in different J2EE frameworks like JSF,Spring,Hibernate, Smart Client, Oracle SOA, Weblogic Portal and many more.
This entry was posted in Hibernate, JSF and tagged , , , , . Bookmark the permalink.

2 Responses to Server side Pagination using JSF Data table and Hibernate

  1. venkat says:

    please help me with one example using jsf 2.0 and richfaces 4.0

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s