使用惰性数据表时,另一个组件不会更新/第二个组件数据落后于一个请求

2024-01-18

我有一个 PrimeFacesp:dataTable并通过实现启用延迟加载LazyDataModel.

dataTable 保存搜索结果,因此在执行搜索请求时,搜索服务仅检索所需的(分页)数据。效果很好。

当使用 ajax 请求时p:commandButton:

<p:commandButton id="searchCmdBtn" value="Search" action="#{searchBean.search}" 
   update=":resultForm:resultList :filterForm:filterMenu :resultForm:messages" 
   ajax="true" />

dataTable 得到正确更新,但 filterForm 中的 filterMenu 没有正确更新(不同的形式,bcz 使用p:layout).

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;
    }

}

过滤器.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() 方法在渲染响应阶段被调用

要了解各个阶段,请阅读BalusC 的 JSF 生命周期教程 http://balusc.blogspot.com/2006/09/debug-jsf-lifecycle.html

显示消息的解决方案(例如:p:messages or p:growl):

  1. RequestContext.getCurrentInstance().update(":growlOrMsgID");
    

    这将不起作用,因为那时添加其他组件进行更新为时已晚。

  2. 找不到具有此名称的数据表属性

  3. 为我工作

  4. 使用RequestContext的PF执行方法 https://stackoverflow.com/questions/16816543/request-update-of-component-with-ajax-from-lazydatamodel-load

    The RequestContext#execute() http://www.primefaces.org/docs/api/3.5/org/primefaces/context/RequestContext.html#execute%28java.lang.String%29当前ajax请求完成后执行javascript。

    为我工作


也可以看看:

  • 从 LazyDataModel.load 请求使用 ajax 更新组件? https://stackoverflow.com/questions/16816543/request-update-of-component-with-ajax-from-lazydatamodel-load
  • 如何处理 Primefaces 延迟加载中的错误? https://stackoverflow.com/questions/11152840/how-to-handle-error-in-primefaces-lazy-load
  • PF 问题:带有 LazyDataModel 的 DataTable 在渲染期间更新模型 http://code.google.com/p/primefaces/issues/detail?id=4945
  • http://itaffinity.wordpress.com/2013/06/08/jsf-displaying-facesmessages-during-render-response-phase/ http://itaffinity.wordpress.com/2013/06/08/jsf-displaying-facesmessages-during-render-response-phase/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用惰性数据表时,另一个组件不会更新/第二个组件数据落后于一个请求 的相关文章

随机推荐