如何从 bean 中显示 p:confirmDialog?

How can I show p:confirmDialog from bean?

我是从 primefaces 得到的,但我不想使用按钮并从 bean 调用确认对话框,我该怎么做或渲染 commandbutton 并从 bean 执行?

问题是我使用命令按钮执行一个方法,这个方法中有一个 if 当为真时我想显示一个确认对话框。

<p:commandButton value="Destroy the World" actionListener="#{dialogView.destroyWorld}" update="message">
    <p:confirm header="Confirmation" message="Are you sure?" icon="ui-icon-alert" />
</p:commandButton>

<p:confirmDialog global="true" showEffect="fade" hideEffect="fade">
    <p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
    <p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
</p:confirmDialog>

您可以执行以下操作

 <p:commandButton value="Destroy the World" onclick="PF('confirmDialogwidget').show();">
 </p:commandButton>



<p:confirmDialog global="true" showEffect="fade" hideEffect="fade" header="Are you sure?"
                     widgetVar="confirmDialogwidget">
    <p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
    <p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" 
                     onclick="PF('confirmDialogwidget').hide();" />
</p:confirmDialog>

或者也使用 bean

<p:commandButton value="Destroy the World" actionListener="#{dialogView.destroyWorld}">
</p:commandButton>


<p:confirmDialog global="true" showEffect="fade" hideEffect="fade" header="Are you sure?"
                 widgetVar="confirmDialogwidget">
    <p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
    <p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" 
                     onclick="PF('confirmDialogwidget').hide();"/>
</p:confirmDialog>

这里是从bean打开的

 public void destroyWorld(){
    RequestContext.getCurrentInstance().execute("PF('confirmDialogwidget').show();")
}