I have: button on main page and dialog.
我有:按钮在主页和对话框。
A would like to have ViewScope life cycle for managed bean (NewDialog.java), what perform the dialog. In other words: recreate the NewDialog bean while pushing the button, and destroy while closing dialog.
A希望对受管理的bean (NewDialog.java)使用ViewScope生命周期,执行对话框。换句话说:在按下按钮时重新创建NewDialog bean,并在关闭对话框时销毁它。
But NewDialog bean has created while loading main page. How to force bean created only when you press a button?
但是NewDialog bean在加载主页时创建。当你按下一个按钮时,如何强制创建bean ?
<ui:composition
<h:form id="mainForm">
<p:commandButton value="New Dialog"
onclick="newDialogVar.show();"/>
</h:form>
<ui:include src="#{viewScopedBean.page}.xhtml" />
</ui:define>
</ui:composition>
Included page:
包括页面:
<ui:composition ..
<f:view >
<h:form id="formId"
<p:dialog appendToBody="false"
dynamic="true"
modal="true"
widgetVar="newDialogVar">
<p:commandButton value="Ok"
actionListener="#{newDialog.ok}"/>
</h:form>
</p:dialog>
</f:view>
</ui:composition>
Bean:
豆:
@ManagedBean
@ViewScoped
public class NewDialog implements Serializable{
@PostConstruct
protected void postConstruct() {
LOG.info("----------------- PostConstruct -------------------");
}
}
I use: PrimeFaces 3.5 with Mojarra 2.1.22
我使用的是:PrimeFaces 3.5和Mojarra 2.1.22。
Thanks in advance!
提前谢谢!
P.S. : according research I add:
附注:根据研究,我补充道:
<context-param>
<param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
<param-value>false</param-value>
</context-param>
to web.xml.
web . xml。
2 个解决方案
#1
3
This is expected behavior. The <ui:include>
runs during view build time, not during view render time. So even if you conditionally render one of its parents, it will still evaluate its src
attribute during building/restoring of the view. In depth background explanation of "view build time" versus "view render time" can be found in this answer: JSTL in JSF2 Facelets... makes sense?
这是预期行为。
Your concrete functional requirement is unclear, so I can't elaborate the right approach in detail, but fact is, you need to look for an alternate approach if you want to postpone the creation of the bean to display of the dialog. Perhaps you need to split the bean in two ones, one holding the include path and another holding the dialog's data?
您的具体功能需求尚不清楚,因此我无法详细阐述正确的方法,但事实是,如果您想推迟bean的创建以显示对话框,则需要寻找另一种方法。也许您需要将bean拆分为两个,一个保存包含路径,另一个保存对话框的数据?
#2
0
I have a situacion like you to show a graphic inside dialog, this button call a methot to set src to dialog:
我有一个像你这样的情境,在对话框中,这个按钮叫一个methot来设置src到对话框:
<p:commandButton value="Montos" update=":form2:growl, :form2:displaygraf" oncomplete="montosDialog.show()" actionListener="#{serviciosMB.mostrarGraf}" icon="ui-icon-disk"/>
this is the dialog (in the same page)
这是对话框(在同一页)
<p:dialog id="dialog" header="Estado del monto del contrato" widgetVar="montosDialog" resizable="false">
<p:panel id="displaygraf">
<ui:include src="#{serviciosMB.urlGrafMontos}" />
</p:panel>
</p:dialog>
managedbean serviciosMB:
managedbean serviciosMB:
public void mostrarGraf() throws Exception {
try {
if (this.servicioUtilNew.getContratoUtil().getMontosList().isEmpty()) {
this.urlGrafMontos ="void.xhtml";
JsfUtil.addWarnningMessage("El contrato no tiene montos definidos");
} else {
this.urlGrafMontos ="grafMontosServicios.xhtml";
}
} catch (Exception e) {
JsfUtil.addErrorMessage(e, "Error: addOrdenProdServArr() " + e.getMessage());
}
}
this is grafMontosServicios.xhtml
这是grafMontosServicios.xhtml
<h:body>
<p:barChart id="stacked1" value="#{grafMontosServiciosMB.categoryModelChartMontos}" legendPosition="ne" animate="true"
title="Estado del monto del contrato" barMargin="20" style="height:300px; width: 500px"/>
</h:body>
managed bean where graphic is built
创建图形的托管bean。
public CartesianChartModel getCategoryModelChartMontos() {
return categoryModelChartMontos;
}
public void setCategoryModelChartMontos(CartesianChartModel categoryModelChartMontos) {
this.categoryModelChartMontos = categoryModelChartMontos;
}
/**
* Creates a new instance of GrafMontosServiciosMB
*/
public GrafMontosServiciosMB() {
}
@PostConstruct
public void crearTablaMontos() {
try {
...
} catch (Exception e) {
JsfUtil.addErrorMessage(e, "Error: createCategoryModel() " + e.getMessage());
}
}
}
}
#1
3
This is expected behavior. The <ui:include>
runs during view build time, not during view render time. So even if you conditionally render one of its parents, it will still evaluate its src
attribute during building/restoring of the view. In depth background explanation of "view build time" versus "view render time" can be found in this answer: JSTL in JSF2 Facelets... makes sense?
这是预期行为。
Your concrete functional requirement is unclear, so I can't elaborate the right approach in detail, but fact is, you need to look for an alternate approach if you want to postpone the creation of the bean to display of the dialog. Perhaps you need to split the bean in two ones, one holding the include path and another holding the dialog's data?
您的具体功能需求尚不清楚,因此我无法详细阐述正确的方法,但事实是,如果您想推迟bean的创建以显示对话框,则需要寻找另一种方法。也许您需要将bean拆分为两个,一个保存包含路径,另一个保存对话框的数据?
#2
0
I have a situacion like you to show a graphic inside dialog, this button call a methot to set src to dialog:
我有一个像你这样的情境,在对话框中,这个按钮叫一个methot来设置src到对话框:
<p:commandButton value="Montos" update=":form2:growl, :form2:displaygraf" oncomplete="montosDialog.show()" actionListener="#{serviciosMB.mostrarGraf}" icon="ui-icon-disk"/>
this is the dialog (in the same page)
这是对话框(在同一页)
<p:dialog id="dialog" header="Estado del monto del contrato" widgetVar="montosDialog" resizable="false">
<p:panel id="displaygraf">
<ui:include src="#{serviciosMB.urlGrafMontos}" />
</p:panel>
</p:dialog>
managedbean serviciosMB:
managedbean serviciosMB:
public void mostrarGraf() throws Exception {
try {
if (this.servicioUtilNew.getContratoUtil().getMontosList().isEmpty()) {
this.urlGrafMontos ="void.xhtml";
JsfUtil.addWarnningMessage("El contrato no tiene montos definidos");
} else {
this.urlGrafMontos ="grafMontosServicios.xhtml";
}
} catch (Exception e) {
JsfUtil.addErrorMessage(e, "Error: addOrdenProdServArr() " + e.getMessage());
}
}
this is grafMontosServicios.xhtml
这是grafMontosServicios.xhtml
<h:body>
<p:barChart id="stacked1" value="#{grafMontosServiciosMB.categoryModelChartMontos}" legendPosition="ne" animate="true"
title="Estado del monto del contrato" barMargin="20" style="height:300px; width: 500px"/>
</h:body>
managed bean where graphic is built
创建图形的托管bean。
public CartesianChartModel getCategoryModelChartMontos() {
return categoryModelChartMontos;
}
public void setCategoryModelChartMontos(CartesianChartModel categoryModelChartMontos) {
this.categoryModelChartMontos = categoryModelChartMontos;
}
/**
* Creates a new instance of GrafMontosServiciosMB
*/
public GrafMontosServiciosMB() {
}
@PostConstruct
public void crearTablaMontos() {
try {
...
} catch (Exception e) {
JsfUtil.addErrorMessage(e, "Error: createCategoryModel() " + e.getMessage());
}
}
}
}