7
votes

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?

4
What have you tried so far? For simply displaying a dialog you can use Primefaces p:dialog.Matt Handy
Hi Matt, I need to do that from server side. if (VersionExists) { show a confirmation dialog.}neni

4 Answers

3
votes

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

You have two options:

  1. Get overwrite permission before calling your action method e.g. with a checkbox "Overwrite file if exists?" or

  2. 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.

2
votes

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.

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

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.

-1
votes

Case

Validate that there is a selected item by pressing the Delete button. If you open a popup, if not shown in the error message, growl.

Alternative: (Sorry I had trouble posting the code)

XHTML: View xhtml

TestManageBean: Manage bean

MORE INFO: www.primefaces.org/showcase/ui/dialogLogin.jsf