How to display a confirm dialog in JSF?

Kunal Jamdade picture Kunal Jamdade · Sep 23, 2015 · Viewed 7.1k times · Source

When I first upload a file in the database and later if user uploads the same file I want to show user a confirm dialog saying "Do you want to override? Yes or No". How should I achieve this?

Answer

Omar picture Omar · Sep 23, 2015

Try this, assuming the view's code looks like :

<p:commandButton value="Upload" action="#{bean.save}" ajax="false" />

<p:confirmDialog widgetVar="confirmDlg" message="Do you want to override the file ?" >
    <p:commandButton value="Yes" action="#{bean.overrideFile()} "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>

And in the managed bean :

public void save(){
    // condition about the file existence
    // if true 
    RequestContext.getCurrentInstance().execute("PF('confirmDlg').show();");
}

...

public void overrideFile(){
    // override the existent file here
}