简体   繁体   中英

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

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM