How to display a confirmation dialog(Primefaces) from backing bean

I have an import function which will parse the XML file which contains the version information of the document and save it in database. If user try to upload the already existing version, I need to show the confirmation dialog like " Version already exists do you wants to overwrite..?" ok, Cancel.

I am using Mozarra 2.0.3, Prime faces 2.2 RC2, Glass Fish 3 and I am trying this way.

<h:form id="conDialog">
    <p:commandButton value="getConfirmMsg" update="conDialog" action="#{buttonBean.getConfirmMsg()}" 
        oncomplete="confirmation.show()"/>
    <p:growl id="messages1" globalOnly="true"/>
    <p:confirmDialog message="Version already exists. Do you want to override it?"
        rendered="#{buttonBean.showConfirm}"
        header="Version already exist" severity="alert" widgetVar="confirmation">
        <p:commandButton value="OK" update="messages1" oncomplete="confirmation.hide()"
            action="#{buttonBean.overrideVersion}" />
        <p:commandButton value="Cancel" onclick="confirmation.hide()" type="button" />
    </p:confirmDialog>
</h:form>

BackingBean

@ManagedBean
@RequestScoped
public class ButtonBean {

    boolean showConfirm = false;

    public boolean isShowConfirm() {
        return showConfirm;
    }

    public void setShowConfirm(boolean showConfirm) {
        this.showConfirm = showConfirm;
    }

    public void overrideVersion() {
        System.out.println("Version alrady exists...Overriding...");
        FacesMessage msg = new FacesMessage("Action is successful");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void getConfirmMsg() {
        System.out.println("Inside getConfirmMsg()....");
        showConfirm = true;
        System.out.println("showConfirm: " + showConfirm);
    }
}

When I click on "OK" the action is not firing. Is there any mistake in the above code?


It's not possible to get confirmation from the client during processing on server.

You have two options:

  • Get overwrite permission before calling your action method eg with a checkbox "Overwrite file if exists?" or

  • You have to stop processing, set a flag and return null to reload current page in browser. Then you could display the p:dialog depending on flag status.


  • You are facing in typical Primefaces Problem.

    When your page is displayed and buttonBean.showConfirm = false, this element is not rendered. This means, it will not appear in the DOM-Tree. No matter what you do afterwards, a non existing element cannot be shown or hidden.

    There are actually two ways to solve you problem.

  • Use a remote command, so that the not rendered HTML-Code will be transmitted from you server.
  • Use css "display: none" instead of rendered="false".

  • I faced very similar question. The solution I came up with was to split the logic into 2 - first, when the button is pressed use 'action' to prepare the data for the validation and use 'oncomplete' to run a remote command which displays the confirmation dialog in which 'OK' is the real action.

    链接地址: http://www.djcxy.com/p/52246.html

    上一篇: 如何将枚举值传递给wcf webservice

    下一篇: 如何从后台bean显示确认对话框(Primefaces)