PrimeFaces 3.3 dataTable に並べ替えを追加したいと考え、リストを格納する次の ViewScoped Bean を作成したため、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;
}
}
これは、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>
問題は、並べ替えのために列ヘッダーをクリックできることですが、最初の並べ替えが機能していなくても、テーブルは並べ替えられないままです。getList() メソッドにブレークポイントを設定すると、リクエストの処理中にリストが EJB から数回フェッチされることがわかります。ビューが ViewScope によってアクティブである限り、Bean を保存する必要はありませんか?