PrimeFaces dialog lazy loading (dynamic=“true”) does not work?

冷暖自知 提交于 2019-11-29 22:17:30

问题


I have recently changed all my beans from RequestScoped to ViewScoped. Suddenly, the lazy loading of dialogs does not work. I am using PrimeFaces JSF library.

<html>
<h:body>
<f:view>    
    <h:form>
        <p:commandButton id="addId" value="Add" title="Add" type="button" onclick="dlgMultiFileSelect.show();"/>
        ...
    </h:form>    
    <p:dialog header="Dialog" widgetVar="dlgMultiFileSelect" modal="true" resizable="true" dynamic="true"> 
        <ui:include src="/dialogs/media_browser.xhtml"/>
    </p:dialog>
</f:view>   
</h:body>
</html>

Seems like dynamic="true" does not work since the backing bean in media_browser.xhtml gets initialized immediately, and not when button is clicked.

Am I doing something wrong?

Using PrimeFaces 3.5.0.


回答1:


have found some pretty easy workaround:

Let's quote BalusC's answer on this other question: Skip executing <ui:include> when parent UI component is not rendered

The <ui:include> runs as being a taghandler during the view build time, while the rendered attribute is evaluated during the view render time.

Regarding his first proposal:

  1. Use a view build time tag like <c:if> around the <ui:include> part.

So first add some new method to your bean. (may even be useful in some application scoped bean for reuse)

public boolean isAjaxRequest() {
    boolean val = FacesContext.getCurrentInstance().getPartialViewContext().isAjaxRequest();
    return val;
}

Then enclose your <ui:include> inside following <c:if>:

<p:dialog id="yourDlg" header="Dialog" widgetVar="dlgMultiFileSelect" modal="true" resizable="true" dynamic="true"> 
    <c:if test="#{applicationBean.ajaxRequest}">
        <ui:include src="/dialogs/media_browser.xhtml"/>
    </c:if>
</p:dialog>

So at page load time, the request isn't ajax... and on other ajax requests on base page, the dialog won't be updated... But if you trigger your dialog

<p:commandButton id="addId" value="Add" title="Add" type="button" 
    oncomplete="dlgMultiFileSelect.show();" update=":yourDlg"/>

and update it's content, it will finally be rendered!

Please note following changes: The p:dialog id, p:commandButton update and p:commandButton oncomplete




回答2:


Facelet tag ui:include will get processed earlier in the cycle and hence it is getting initialized. If you want to update the content of the dialog on button click, you need to do so using the update="id of the dialog" on your commandButton. You can use for your ui:include so that the page is not loaded initially.



来源:https://stackoverflow.com/questions/16541162/primefaces-dialog-lazy-loading-dynamic-true-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!