欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JSF的服务器端分页的实现

程序员文章站 2022-07-12 15:20:04
...

相信对JSF有一定了解的童鞋,都知道怎么用dataGrid显示数据,一般情况我都是用的都是客户端分页,闲来无事按照例子写了个服务器端分页,话不多说,贴代码:

@Inject(value = "employeeSpringService")
private IEmployeeService es;


@Bind(id = "grid", attribute = "value")
	private GridDataProvider dataProvider = new CachedPagedDataProviderAdapter(
			new CachedPagedDataProvider() {
	@Override
	public Object getData(int start, int limit) {
		     return es.limitQuery(start, limit).toArray();
	            }
	@Override
	public int getTotalCount() {
	            List list=es.getTotal();
	            return ((Long)list.get(0)).intValue();
	  }

});

 

public List<Employee> limitQuery(int start,int limit) {
	logger.debug("finding the queried Employee instances");
        try {
        	DetachedCriteria dc=DetachedCriteria.forClass(Employee.class);
	List<Employee> results = (List<Employee>) getHibernateTemplate().findByCriteria(dc, start, limit);
	logger.debug("find queried successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            logger.error("find queried failed", re);
            throw re;
        }
	}
	
public List getTotal() {
	logger.debug("finding the queried Employee instances");
        try {
        	 List  results =getHibernateTemplate().find("select count(*) FROM Employee");
                logger.debug("find queried successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            logger.error("find queried failed", re);
            throw re;
        }
	}

 主要就是getHibernateTemplate().findByCriteria()方法。。。

相关标签: JSF