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

大兔子大兔子 提交于 2019-12-19 02:13:53

问题


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?


回答1:


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 build view. 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.




回答2:


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.




回答3:


We had some problems with @ViewScoped in the JBoss 7.1/Mojarra 2.1.7 and we changed to Omnifaces

I suggest you to use @org.omnifaces.cdi.ViewScoped instead of @javax.faces.bean.ViewScoped

I tested with both your example and here on the log you can see the difference:

Log with @org.omnifaces.cdi.ViewScoped

18:58:40,887 INFO  [xxx.TheForm] (http-localhost-127.0.0.1-8080-2) @postconstruct
18:58:40,890 INFO  [xxx.TheForm] (http-localhost-127.0.0.1-8080-2) openUpload()

Log with @javax.faces.bean.ViewScoped

19:01:19,753 INFO  [xxx.TheForm] (http-localhost-127.0.0.1-8080-5) @postconstruct
19:01:19,753 INFO  [xxx.TheForm] (http-localhost-127.0.0.1-8080-5) @postconstruct
19:01:19,754 INFO  [xxx.TheForm] (http-localhost-127.0.0.1-8080-5) openUpload()


来源:https://stackoverflow.com/questions/26492247/will-primefaces-dialog-framework-break-view-scope-as-compared-to-pdialog

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