我的问题是关于最佳实践,如何在JSF应用程序中对结果集(例如SQL结果)实现分页?
数据结果集通常由@RequestScoped bean实现,如以下简短示例所示:
@Named
@RequestScoped
public class DataBean {
private List<Item> items;
@EJB
private ItemService itemService;
@PostConstruct
public void init() {
items = itemService.list();
}
public List<Item> getItems() {
return items;
}
}
这样,结果集在bean的整个生命周期中只计算一次。另请参见以下讨论:How and when should I load the model from database for h:dataTable
现在,如果您只想显示数据的子集,则如何实现分页-例如10条记录时间。为此,就我的理解而言,您需要一个ViewScoped或ConversationScoped Bean,其中包含用户选择的当前页面索引。
@Named
@ConversationScoped
public class PageController implements Serializable {
private int pageIndex = 0;
@PostConstruct
protected void init() {
if (conversation.isTransient()) {
conversation.begin();
}
}
public void next() {
pageIndex++;
}
public void back() {
pageIndex--;
if (pageIndex < 0) {
pageIndex = 0;
}
}
public int getPageIndex() {
return pageIndex;
}
}
我的问题是:如何为我的DataBean提供来自PageController的pageIndex信息。或者更确切地说,最佳实践是什么? 我想避免实现DataBean ViewScoped,因为我不想将数据结果存储在服务器会话中。
到目前为止,我唯一的解决方案是将数据加载到viewAction中,并为pageController提供一个方法参数:
<f:metadata>
<f:viewAction action="#{dataBean.loadData(pageController)}" />
</f:metadata>