p:commandButton p:confirm actionListener not triggering

6.6k views Asked by At

Primefaces actionListener is not working in p:confirmDialog . Can someone help? The saveRecord works fine in a simple dialog box.

<p:commandButton value="Save" actionListener="#{EmployeeDetailsComponent.displayInfo}">
    <p:confirm header="Attention" message="test" icon="ui-icon-alert" />
</p:commandButton>

<p:confirmDialog global="true" showEffect="fade" hideEffect="explode" appendToBody="true">
    <p:commandButton value="Save"  action="#{TestComponent.saveRecord(TestComponent.testData)}" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
    <p:commandButton value="Cancel" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
</p:confirmDialog>
3

There are 3 answers

3
Bikram On

remove appendToBody="true" from the confirm dialog component and enclose Save command buttton with in form.

<!DOCTYPE html>
<f:view xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:p="http://primefaces.org/ui">
    <html lang="en">
        <h:head>
        </h:head>
        <h:body id="top">
            <h:form>
                <p:commandButton value="Save" actionListener="#{simpleBean.displayInfo}" action="#{simpleBean.saveRecord}">
                    <p:confirm header="Attention" message="test" icon="ui-icon-alert" />
                </p:commandButton>
            </h:form>

            <p:confirmDialog closeOnEscape="true" global="true" showEffect="fade" hideEffect="explode">  
                <p:commandButton value="Save" type="button" styleClass="ui-confirmdialog-yes"/>
                <p:commandButton value="Cancel" type="button" styleClass="ui-confirmdialog-no"/>
            </p:confirmDialog>

        </h:body>
    </html>

</f:view>

SimpleBean.java

@Named("simpleBean")
public class SimpleBean implements Serializable{
   public void displayInfo(){
      Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Display Info works");        
   }
   public String saveRecord(){
      Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Save Record works");        
      return null;
   }
}
2
Predrag Maric On

You have appendToBody="true", which means there is no <h:form> to handle the click because the whole dialog content is moved directly under body. Just add the form inside the dialog.

<p:confirmDialog global="true" showEffect="fade" hideEffect="explode" appendToBody="true">    
    <h:form>
        <p:commandButton value="Save"  action="#{TestComponent.saveRecord(TestComponent.testData)}" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
        <p:commandButton value="Cancel" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />      
    </h:form>    
</p:confirmDialog>
0
Jucai On

I just add a div to center the buttons and the process and the action works perfectly, example code with primefaces 5.1:

 <p:confirmDialog global="true" showEffect="clip"  >
      <div align="center">
         <p:commandButton value="Aceptar" actionListener="#{bienvenidoUI.asignarPSI()}"  update="tablaProceso" icon="ui-icon-check"   process="@this" styleClass="ui-confirmdialog-yes"/>
         <p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
      </div>
 </p:confirmDialog>

and I just call the confirmDialog from inside a table with:

<p:commandButton   icon="ui-icon-circle-check" title="Asignar PSI"  rendered="#{bienvenidoUI.usr.idUsuario==bienvenidoUI.autorizadorId and min.estado.descripcion=='Proceso'}">
       <f:setPropertyActionListener value="#{min}" target="#{bienvenidoUI.minutaSelected}"  />
       <p:confirm header="ConfirmaciĆ³n" message="Esta seguro que desea asignar psi?" icon="ui-icon-alert" />
</p:commandButton>