我有一个PrimeFaces p:dataTable
,并通过实现LazyDataModel
启用了延迟加载。
dataTable保存搜索结果,因此在执行搜索请求时,搜索服务仅检索所需的(分页)数据。很好
当使用p:commandButton
进行ajax请求时:
<p:commandButton id="searchCmdBtn" value="Search" action="#{searchBean.search}"
update=":resultForm:resultList :filterForm:filterMenu :resultForm:messages"
ajax="true" />
dataTable会正确更新,但filterForm中的filterMenu不会正确更新(不同形式,使用
p:layout
bcz)。filterMenu是后面的一个请求。这意味着当我再次按下搜索按钮时,filterMenu会用t更新,只有在第二个ajax请求之后才更新
Bean
@ManagedBean
@ViewScoped
public class SearchBean implements Serializable {
private LazyDataModel<Entity> lazyDataModel;
private MenuModel filterMenuModel = new DefaultMenuModel();
private SearchResult searchResult = null;
public void search() {
// lazy call
getLazyDataModel();
if (searchResult != null) {
buildFilterMenu(searchResult);
}
}
private void initializeDataModel() {
lazyDataModel = new LazyDataModel<Entity>() {
private static final long serialVersionUID = 1L;
@Override
public List<Entity> load(int first, int pageSize, List<SortMeta> multiSortMeta, Map<String, String> filters) {
// handling sorting and filtering
// get search results
try {
setSearchResult(searchService.getEntities(queryText, selectedQueryOperand, getFilterOptions(), first, (first + pageSize), multiSortMeta));
} catch (Exception e) {
// handle exception
}
if (searchResult == null) {
return null;
}
List<Entity> resultEntities = searchResult.getResultEntities();
// total count
this.setRowCount((int) searchResult.getTotalSize());
return resultEntities;
}
// other override-methods
};
}
public void buildFilterMenu() {
// builds the filterMenu depending on searchResults
}
// getters and setters
public LazyDataModel<Entity> getLazyDataModel() {
if (lazyDataModel == null) {
initializeDataModel();
}
return lazyDataModel;
}
}
filters.xhtml
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:fn="http://java.sun.com/jsp/jstl/functions">
<p:panelMenu id="filterMenu" model="#{searchBean.filterMenuModel}" />
</ui:composition>
最佳答案
在搜索PF论坛后,我找到了根本原因:
在渲染响应阶段调用dataTable Lazy load()方法
要了解阶段,请阅读this tutorial on JSF lifecycle from BalusC
显示消息的解决方案(例如p:messages
或p:growl
):
RequestContext.getCurrentInstance().update(":growlOrMsgID");
这将不起作用,因为当时添加其他组件进行更新为时已晚。
找不到与此名称对应的dataTable
为我工作
当前的ajax请求完成后,RequestContext#execute()执行一个javascript。
为我工作
另请参见: