Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 779
neni Avatar asked Apr 29 '11 12:04

neni


2 Answers

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.

like image 143
Matt Handy Avatar answered Sep 27 '22 18:09

Matt Handy


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".
like image 21
Hannes Avatar answered Sep 27 '22 18:09

Hannes