Primefaces 5.0,JSF 2.2,Wildfly 8.1
以下用例:
bean1.java:
module Main where
fib :: Integer -> Integer
fib 0 = 1
fib 1 = 1
fib x = fib (x - 1) + fib (x - 2)
一切都很好。 Dialog1出现。
dialog1.xhtml:
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);
}
}
bean2.java:
<h:body>
<h:form>
<p:commandButton value="Button" actionListener="#{bean2.dialog1ButtonClicked}" />
</h:form>
</h:body>
dialog2.xhtml:
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);
}
}
Dialog2显示在dialog1!
中我在打开dialog2之前尝试用Primefaces Dialog Framework关闭dialog1:
<h:body>
The operation was successful.
</h:body>
Dialog2没有显示。
我尝试在AJAX回调RequestContext.getCurrentInstance().closeDialog(null);
RequestContext.getCurrentInstance().openDialog("dialog2.xhtml", options, null);
之后打开dialog2
Dialog2没有显示。
我尝试了客户端Java Script调用:<p:ajax event="dialogReturn" listener="#{bean1.dialogClosed}"/>
Dialog2仍然显示嵌套到dialog1。
答案 0 :(得分:2)
解决方案:
RequestContext.getCurrentInstance().openDialog("dialog1.xhtml", options, null);
dialog1.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>
对话框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 ...
}
关闭对话框的另一个bean:
@ManagedBean
@RequestScoped
public class Bean2 implements Serializable {
public void closeRegistration() {
FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove("bean");
}
}
方法 closeRegistration 删除了viewscoped bean。因此,在同一页面内对对话框的另一次调用将从头开始对话流程。
答案 1 :(得分:2)
使用onHide
您可以打开另一个对话框。
<p:dialog class="userRegisterDialog" header="#{bean.dailogHeader}" modal="true" resizable="false" draggable="false" onHide="PF('dialog2').show();>
</p:dialog>
使用转义键关闭对话框时也能正常工作。
答案 2 :(得分:-1)
我们也可以在import javax.swing.JButton;
import javax.swing.JFrame;
public class JButtonExample {
JButtonExample(){
JFrame frame=new JFrame();
// Creating Button
JButton b=new JButton("Click Me..");
b.setBounds(50,50,90, 50);
//Adding button onto the frame
frame.add(b);
// Setting Frame size. This is the window size
frame.setSize(300,200);
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonExample();
}
}
或onsuccess
或onerror
的对话框中关闭。查看图片以供参考