I wanted to add sorting to a PrimeFaces 3.3 dataTable and created the following ViewScoped bean which stores the list so it is not fetched all the time from the EJB:
@Named
@ViewScoped
public class CustomerList implements Serializable {
private static final long serialVersionUID = 6168621124401208753L;
@EJB
CustomerEJB customerBean;
List<Customer> allCustomers = null;
public void loadCustomerList() {
allCustomers = customerBean.findAll();
}
public List<Customer> getList() {
if (allCustomers == null) {
loadCustomerList();
}
return allCustomers;
}
}
and this is the view using the bean:
<ui:composition template="/WEB-INF/templates/template.xhtml">
<ui:define name="content">
<h:form id="customerList">
<p:dataTable id="customer" var="customer"
value="#{customerList.list}" sortBy="#{customer.id}"
paginator="true" rows="10" paginatorAlwaysVisible="false"
paginatorPosition="top">
<p:column sortBy="#{customer.id}">
<f:facet name="header">
<h:outputText value="#{msg.customerIdLabel}" />
</f:facet>
<h:outputText value="#{customer.id}" />
</p:column>
<p:column sortBy="#{customer.lastName}">
<f:facet name="header">
<h:outputText value="#{msg.customerLastNameLabel}" />
</f:facet>
<h:outputText value="#{customer.lastName}" />
</p:column>
The issue is that i can click the column headers for sorting, but the table remains unsorted, even the initial sorting is not working. When u set a breakpoint in the getList() method i can see that the list is fetched several times from the EJB during when a request is processed. Shouldn't the bean be stored as long as the view is active by ViewScope?