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">  
    <f:param value="#{item.value.ID}" name="editId"/>
    <h:graphicImage value="./edit.png" />
</p:commandLink>

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

<p:dialog id="editDialog" widgetVar="dlgEditWF" dynamic="true">
    <ui:include src="edit/edit_watchfolder.xhtml"/>
</p:dialog> 

I am using dynamic=="true" to prevent edit_watchfolder.xhtml from being loaded before the button is clicked.

editWFForm is a form in edit_watchfolder.xhtml.

Problem is that when I launch the dialog for the first time, the edit_watchfolder.xhtml backing bean (request scoped) gets created twice. First time the value passed with editId (see above code) is OK, but second time it is not there, so the backing bean gets wrong values.

After I go back (OK or Cancel buttons) to the parent page and launch the dialog again, the problem is gone, all is OK. The problem seems to be only the first launch.

Why does the backing bean get created twice first time dialog is launched?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Danijel
  • 8,198
  • 18
  • 69
  • 133

2 Answers2

1

The reason for this is because first time you open dialog AJAX request is sent to load actual content. The only reasonable here is to use @ViewScoped bean which will handle both requests: first which is fired by commandButton and seconds which is fired by dlgEditWF.show(). Of course if you remove dynamic="true" there will be just one request.

partlov
  • 13,789
  • 6
  • 63
  • 82
1

I suspect that this is because of how the Primefaces dynamic functionality works.

The first request by the commandButton will send the request parameter with the editID to load. After this request occurs and the response is received and the page elements are updated, the oncomplete javascript occurs which displays the dialog.

Once the dialog is displayed an entirely different request occurs to dynamically load the contents of the dialog, except this time there is no editID parameter. Your bean is @RequestScoped therefore it is being created twice and the second time it does not retain the editID request parameter.

There are three possible solutions:

  1. Remove the dynamic attribute from your dialog

  2. Change your bean to @ViewScoped or @SessionScoped.

  3. Change the editID request parameter to a bean property in a @SessionScoped managed bean.

maple_shaft
  • 10,435
  • 6
  • 46
  • 74
  • 1
    Thanks. The problem goes away if instead of `oncomplete` I use `onstart`. In that case dialog appears immediately - but empty - then after a secodn or two it gets updated with ajax to proper values. So, `onclick` works, but user experience is really bad. – Danijel Feb 21 '13 at 13:04