Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will PrimeFaces Dialog Framework break view scope as compared to p:dialog?

I'm using PrimeFaces 5.

I would like to open a dialog when a button is pressed.

<p:commandButton value="add upload" actionListener="#{theForm.openUpload}" >

public void openUpload() {
    this.item = new Item();
    RequestContext.getCurrentInstance().openDialog("uploadForm");
}

There will be a save button in the dialog to save the inputs.

<h:commandButton value="#{text['button.add']}" id="add" styleClass="btn btn-default" actionListener="#{theForm.confirmAdd}"/>

public void confirmAdd() {
    RequestContext.getCurrentInstance().closeDialog("uploadForm");
}

My managed bean is @ViewScoped. Will the command button break the view scope if the dialog is in an external file as done by PrimeFaces Dialog Framework? Whenever I click the "add upload" button, the @PostConstruct method is called again just like the scope is lost.

Comments section of the official blog says it won't break the view scope, but here the forum a core developer says openDialog() creates a new view, therefore it breaks the view scope.

Can someone confirm this?

like image 533
Yichaoz Avatar asked Oct 21 '14 17:10

Yichaoz


2 Answers

PrimeFaces' Dialog Framework basically shows another view in an <iframe> tag. I wouldn't call that breaking a view scope, but the dialog view will have it's own scope, because it is practically a different page. That may or may not be desirable in different circumstances. As PrimeFaces' User Guide says:

Dialog Framework (DF) is used to open an external xhtml page in a dialog that is generated dynamically on runtime.

p:dialog

  • Exists in the same view scope.
  • Can easily have the same conversation context.
  • Statically defined, the dialog and its components get created immediately on view build. You can only delay rendering, e.g. with a dynamic=true.
  • Declarative definition means it's more readable and maintainable, because the dialog's existence is not hidden somewhere in java code.

Dialog Framework

  • Has its own view scope.
  • Developer has to worry about passing parameters, propagating conversation context. (And PF didn't support includeViewParams until 5.1.)
  • Dynamic creation means dialog and its components won't be created until the dialog is actually opened, but a new dialog will be created each time it's opened. If the dialog is opened many times, the total performance hit will be larger, plus many dialog views can exhaust JSF view limit and expire other views.
  • Imperative dynamic creation allows for more resource efficiency in certain cases. E.g. show one specific dialog out of dozens based on user input. Or a rarely used dialog, which could be opened from any page of an application.

My recommendation would be to use p:dialog by default. Only use Dialog Framework in cases like I mentioned in the last bullet point.

like image 188
Vsevolod Golovanov Avatar answered Nov 13 '22 07:11

Vsevolod Golovanov


That's normal because you have already instantiated theForm(ManagedBean) in your main.xhtml. SO the scope is already used within the main.xhtml. When you clicked to open the dialog: the dialog is a new view, then a new instance of theForm (ManagedBean) is created.

like image 44
Dhouha BOUSSALEM Avatar answered Nov 13 '22 06:11

Dhouha BOUSSALEM