Chr*_*ris 5 jsf dialog primefaces jsf-2
Primefaces 5.0、JSF 2.2、Wildfly 8.1
以下用例:
bean1.java:
public void buttonClicked() {
Map<String, Object> options = new HashMap<>();
options.put("modal", true);
options.put("widgetVar", "dialog1");
options.put("id", "dlg1");
if(somethingTrue()) {
RequestContext.getCurrentInstance().openDialog("dialog1.xhtml", options, null);
}
}
Run Code Online (Sandbox Code Playgroud)
一切都很好。Dialog1 出现。
对话框1.xhtml:
<h:body>
<h:form>
<p:commandButton value="Button" actionListener="#{bean2.dialog1ButtonClicked}" />
</h:form>
</h:body>
Run Code Online (Sandbox Code Playgroud)
bean2.java:
public void dialog1ButtonClicked() {
Map<String, Object> options = new HashMap<>();
options.put("modal", true);
options.put("widgetVar", "dialog2");
options.put("id", "dlg2");
if(somethingTrue()) {
RequestContext.getCurrentInstance().openDialog("dialog2.xhtml", options, null);
}
}
Run Code Online (Sandbox Code Playgroud)
对话框2.xhtml:
<h:body>
The operation was successful.
</h:body>
Run Code Online (Sandbox Code Playgroud)
Dialog2 显示在dialog1 中!
在打开dialog2之前,我尝试使用Primefaces对话框框架关闭dialog1:
RequestContext.getCurrentInstance().closeDialog(null);
RequestContext.getCurrentInstance().openDialog("dialog2.xhtml", options, null);
Run Code Online (Sandbox Code Playgroud)
Dialog2 不显示。
我尝试在 AJAX 回调<p:ajax event="dialogReturn" listener="#{bean1.dialogClosed}"/>
Dialog2 不显示后打开dialog2。
我尝试了客户端 JavaScript 调用:onclick="PF('dialog1').hide()"
Dialog2 仍然嵌套在dialog1 中显示。
解决方案:
RequestContext.getCurrentInstance().openDialog("dialog1.xhtml", options, null);
对话框1.xhtml:
<p:dialog class="userRegisterDialog" header="#{bean.dailogHeader}"
modal="true" resizable="false" draggable="false">
<p:ajax event="close" listener="#{bean2.closeRegistration}" update=":update"/>
<p:panel id="update">
<p:panel id="step1" rendered="#{bean.showStep1}">
<h:form>
<p:commandButton class="continueButton" value="Button1" actionListener="#{bean.doStep1}" update=":update"/>
</h:form>
</p:panel>
<p:panel id="step2" rendered="#{bean.showStep2}">
<h:form>
<p:commandButton class="closeButton" value="Button2" actionListener="#{bean.doStep2}" update=":update"/>
</h:form>
</p:panel>
</p:panel>
</p:dialog>
Run Code Online (Sandbox Code Playgroud)
对话框 bean:
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
@ManagedBean
@ViewScoped
public class Bean implements Serializable
{
private boolean showStep1 = true;
private boolean showStep2 = false;
public void doStep1(ActionEvent actionEvent) {
if(doSomething())
{
setShowStep1(false);
setShowStep2(true);
}
}
public void doStep2(ActionEvent actionEvent) {
if(doSomething2())
{
RequestContext.getCurrentInstance().closeDialog(null);
}
}
// Getter and setter ...
}
Run Code Online (Sandbox Code Playgroud)
用于关闭对话框的另一个 bean:
@ManagedBean
@RequestScoped
public class Bean2 implements Serializable {
public void closeRegistration() {
FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove("bean");
}
}
Run Code Online (Sandbox Code Playgroud)
closeRegistration方法删除 viewscoped bean。因此,在同一页面内再次调用该对话框将从头开始对话流程。
归档时间: |
|
查看次数: |
26852 次 |
最近记录: |