I have a application using JSF2.0, Spring3 and Hibernate4.
I am displaying values in Primefaces 3.4.2 datatable, the problem is when I click the pagination, datatable rows always remain in first 10 records. It doesn’t displays next 10 records.
My datatable code
<h:form>
<p:dataTable id="dataTable" var="req" value="#{reqMB.myList}"
paginator="true" rows="10"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5,10,15">
<f:facet name="header">
</f:facet>
<p:column>
<f:facet name="header">
<h:outputText value="XXX" />
</f:facet>
<h:outputText value="#{req.bbbb}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="XXXXS" />
</f:facet>
<h:outputText value="#{req.kkjj}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="XXXXXOO" />
</f:facet>
<h:outputText value="#{req.nnnn}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="XXXXKK" />
</f:facet>
<h:outputText value="#{req.kkkk}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="XKKKK" />
</f:facet>
<h:outputText value="#{req.pppp}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="LLLL" />
</f:facet>
<h:outputText value="#{req.llll}" />
</p:column>
</p:dataTable>
</h:form>
ManagedBean
@Named("reqMB")
@Scope("request")
public class MyBean implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
MyService myService;
List<MyClass> myList;
public List<MyClass> getMyList() {
try {
myList= new ArrayList<MyClass>();
myList.addAll(getMyService().getMymethod());
} catch (Exception e) {
e.printStackTrace();
}
return myList;
}
How can I resolve this issue?
Update 1
I have noticed that when I display few number of records pagination works fine, but when I display records more than 1300, then pagination doesn’t work.
I just tried 30000 records and it worked ok. One thing worth noticing though is that the getMyList method gets called many times (6 in my case in render response phase)and each time it is called your bean fetch/generate a brand new list, and that may be the part that is causing the problem (Although I tried to generate a new list in my getter method but it worked OK).
Generally speaking it is recommended not to put any business logic related codes there. Instead in many cases it would be better to populate the list in your @PostConstruct method or somewhere else. Please see the post made by BalusC and it may be helpful.
Why JSF calls getters multiple times
Also you can read this for lazy loading
Efficient JSF Pagination
Below are my test codes:
And this is the backing bean:
And finally the model class: