p:commandButton 的 p:confirm 属性未触发 actionListener。

3

Primefaces的actionListener在p:confirmDialog中无法工作。有人能帮忙吗?在简单对话框中,saveRecord运行正常。

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

请查看以下帖子。 https://dev59.com/1YHba4cB1Zd3GeqPW9_M - Skyware
3个回答

2

我只是添加了一个div来居中按钮,使用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>

我只是从表格中调用confirmDialog:

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

2
您有appendToBody="true",这意味着没有<h:form>来处理单击事件,因为整个对话框内容直接移动到body下面。只需在对话框内添加表单即可。
<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>

@Samantha你的bean是命名为testComponent还是TestComponent?试试小写,或者发布bean代码。 - Predrag Maric
Bean的名称很好。我有一个简单的保存命令,没有对话框,它可以正常工作。问题出在对话框中的操作上。 - Samantha

1

从确认对话框组件中移除 appendToBody="true",并将“保存”命令按钮放在表单内。

<!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;
   }
}

我已在PF5.1和GF4上进行了测试,这是有效的。我已提供完整的页面和Bean供您测试。 - Bikram
是的,上面的代码在不包括“action”命令的情况下可以工作。我需要在保存命令中添加“action”(请参见我的代码)。但是这个“action”没有起作用。 - Samantha
如果您需要添加操作,应将其添加到调用表单的“保存”命令按钮中。请参考更新后的代码。 - Bikram

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接