2

I am using PrimeFaces UI library for my web UI project.

I have a manage_watchfolder.xhtml page that has a button, and this button launches a dialog:

<p:commandButton value="Add" oncomplete="dlgEditWF.show()"
    update=":editWFForm" process="@none"/>  

Inside this same file I have dlgEditWF included from edit_watchfolder.xhtml:

<p:dialog id="editDialog" widgetVar="dlgEditWF" modal="true"
    resizable="true" onShow="showHideActionLocation();">
    <ui:include src="edit/edit_watchfolder.xhtml"/>
</p:dialog> 

The problem is that I do not want edit_watchfolder.xhtml to be loaded before the button is clicked. However, edit_watchfolder.xhtml is "loaded" at the same moment manage_watchfolder.xhtml is created. So, all the beans called from edit_watchfolder.xhtml are created, initialized, etc, even user maybe will never actually click on the button. This creates a lot of overhead, and makes execution slow.

Can I avoid this?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Danijel
  • 8,198
  • 18
  • 69
  • 133
  • 1
    You can find the solution in this answer by BalusC http://stackoverflow.com/questions/11989631/skip-executing-uiinclude-when-parent-ui-component-is-not-rendered – Ravi Kadaboina Feb 14 '13 at 18:23

2 Answers2

5

This is "by design" for the reasons explained in this answer: Skip executing <ui:include> when parent UI component is not rendered.

In your particular case, your best bet is to leave the bean uninitialized by default and perform initialization during the command button's action method instead.

<p:commandButton value="Add" action="#{bean.initDialog}" 
    process="@this" update=":editWFForm" oncomplete="dlgEditWF.show()" />
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • But my "parent" page and pop-up dialog use **the same** bean. :-( – Danijel Feb 18 '13 at 15:28
  • I'm not sure how that forms a problem if you invoke the dialog's initialization by action method only. – BalusC Feb 18 '13 at 15:29
  • Because my parent page needs the bean initialized. My pop-up page needs the been uninitialized. If I move the initialization from contructor to initDialog(), then how will my parent page get initialized bean? – Danijel Feb 18 '13 at 15:32
  • Just do parent page initialization in bean (post)constructor and dialog initialization in action method? They do not need to take place in one and same method. – BalusC Feb 18 '13 at 15:42
  • Problem is that when dialog creates bean it will do parent-page initialization. I can't skip parent page initialization in this case (post construct as you say), can I? – Danijel Feb 18 '13 at 15:50
  • Sorry if I am not clear. Obviously I need two different inits, one for parent page, and other for dialog, mutualy exclusive. Dialog one `initDialog()` I can do with button action, but "default" one will be called for both unfortunately? – Danijel Feb 18 '13 at 16:01
  • The (post)constructor is only invoked when the bean is constructed by JSF. I'm not sure how/why that's a problem. You want that the default page still works, right? – BalusC Feb 18 '13 at 16:02
  • Btw, my bean is request scoped. – Danijel Feb 18 '13 at 16:04
  • I need to avoid "default" constructor when dialog "creates" the bean, that is, before I press the button and call `initDialog()` action. Is that possible? – Danijel Feb 18 '13 at 16:06
  • If you do that, then the chance is big that `` fails to do its job (for sure when it's placed inside e.g. a data table). Can't you just put the bean in the view scope? – BalusC Feb 18 '13 at 16:09
  • no I can't use view scope because I would need to serialize everything... not doable – Danijel Feb 18 '13 at 16:14
  • If you're absolutely positive that the bean doesn't need to be initialized for the parent page during a postback, then add a check if you're in a postback or not. – BalusC Feb 18 '13 at 16:17
  • Maybe I just do different beans for dialog and parent? Parent bean would extend from the dialog bean, and it would add some stuff that I don't want in the dialog bean. Does that make sense? – Danijel Feb 18 '13 at 16:18
  • While a better design, that would functionally not make any difference. The bean associated with the parent page would still be constructed/initialized when you submit the parent page's form, because it's a new request and the bean is request scoped. Depending on the specific functional requirement, it would maybe work for you if you make the dialog lazy loading (`dynamic="true"`) and if you use `` instead of `` so that you don't submit the form. – BalusC Feb 18 '13 at 16:21
4

You can use the "dynamic" attribute of p:dialog. Have a look at the primefaces documentation.

<p:commandButton id="basic" value="Show Dialog" onclick="dlg.show();" type="button" />  

<p:dialog id="dialog" header="Dynamic Dialog" widgetVar="dlg" dynamic="true">  
    <h:outputText value="This content is loaded lazy." />  
</p:dialog>  
Nicolas Labrot
  • 4,017
  • 25
  • 40
  • @BalusC Different problem now: first time that I launch the dialog - backing bean gets created twice. After I close the dialog, and launch again it creates bean only once, and continues creating it once like it should and then works ok. So only problem is FIRST launch. Why does the bean get created twice this first time? – Danijel Feb 21 '13 at 11:38