JSF2 + Primefaces:DataTable延迟加载问题

时间:2013-05-13 07:58:59

标签: jsf jsf-2 primefaces

我想,我的java代码是正确的,但记录没有显示在dataTable上。

请检查以下代码。我不知道我犯了什么错误。

.xhtml

 <p:dataTable value="#{userDetailList.userDataList}" var="user" rows="5" 
                            paginator="true" lazy="true" 
                            paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} 
{CurrentPageReport} {NextPageLink} {LastPageLink}">

                        <p:column headerText="No">
                            <h:outputText value="#{user.id}"></h:outputText>
                        </p:column>

                        <p:column headerText="User Name">
                            <h:outputText value="#{user.userName}"></h:outputText>
                        </p:column>



                    </p:dataTable>

的.java

@PostConstruct
    public void init(){
        // +getter , setter
        userDataList = new LazyDataModel<UserDetail>(){

            private static final long serialVersionUID = 1L;

            @Override
            public List<UserDetail> load(int first, int pageSize, String sortField,
                    SortOrder sortOrder, Map<String, String> filters) {

                return lazyDataModelCom.getResultList(first, pageSize, sortField, sortOrder, filters);
            }
        };
    }

getResultList

public List<UserDetail> getResultList(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
        Session session = HibernateUtils.getsession();

        Criteria criteria = session.createCriteria(UserDetail.class);
        criteria.addOrder(Order.asc("id"));

        for (Map.Entry<String, String> entry : filters.entrySet()) {
            if (entry.getValue() != null) {
                criteria.add(Restrictions.ilike("id",
                        "%" + entry.getValue() + "%"));         }
        }

        criteria.setMaxResults(pageSize);
        criteria.setFirstResult(first); 

        return criteria.list(); 
    }

getResultList 方法返回一个正确的值,我调试它。 我认为问题在 userDataList

1 个答案:

答案 0 :(得分:8)

您的LazyDataModel实施未设置rowCount。你应该做点什么:

public void init(){
    // +getter , setter
    userDataList = new LazyDataModel<UserDetail>(){

        private static final long serialVersionUID = 1L;

        @Override
        public List<UserDetail> load(int first, int pageSize, String sortField,
                SortOrder sortOrder, Map<String, String> filters) {
            userDataList.setRowCount(...);
            return lazyDataModelCom.getResultList(first, pageSize, sortField, sortOrder, filters);
        }
    };
    userDataList.setRowCount(...);
}

注意<{1}}在分页之前的行数,但在过滤之后

请参阅包含一个工作示例的this answer(唯一的区别是它使用JPA Criteria API而不是Hibernate Criteria):