Primefaces - 如何以相同的形式在不同的消息组件中显示不同的消息

Primefaces - How to show different messages in different messages component in the same form

我需要你的助手在正确的消息组件中显示错误消息。在表单中,我使用了两次消息组件标签。第一个消息组件标记称为消息,我向用户显示从主表单验证的任何错误。

我还有一个相同形式的对话框,它将验证变量是空白还是空,如果它是空白,则错误消息应显示在 dialog 中的 messagesPDFST 中不在主要形式。目前任何错误都显示在第一个消息组件中,即使错误在 dialog 中也是消息。这是代码:

<h:form id="Requests">

    <p:messages id="messages" showDetail="true" autoUpdate="true" closable="true"/>

     <p:dialog id="Certificate1" header="Certificate" widgetVar="Certificate1" modal="true">

          <div align="right">
              <p:messages id="messagesPDFST" showDetail="true" autoUpdate="true" closable="true" />

               <p:commandButton value="Download" ajax="false" actionListener="#{hrd.PDFSTAR}" >

                    <p:fileDownload value="#{hrd.fileSTAR}" />
                </p:commandButton>

         </div>

</p:dialog>

</h:form>

和 Bean 代码:

public void PDFSTAR() {
     if (refNo== null || refNo.equals("")) {

    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "Please enter the Reference No."));

            }

}

要显示特定组件的 FacesMessage,您首先需要

  1. 为特定组件排队消息:

    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "Please enter the Reference No."));
    

    第一个参数 null 是您要定位的组件的客户端 id

  2. message 组件上指定 for 属性,将消息显示仅限于该组件。

把它们放在一起,你现在应该有

 FacesContext.getCurrentInstance().addMessage("Requests:Certificate1:downloadButton", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "Please enter the Reference No."));

其中 "downloadButton" 是 <p:commandButton/>

id
<p:messages for="downloadButton" id="messages" showDetail="true" autoUpdate="true" closable="true"/>

<p:commandButton id="downloadButton" value="Download" ajax="false" actionListener="#{hrd.PDFSTAR}" >
      <p:fileDownload value="#{hrd.fileSTAR}" />
</p:commandButton>

与实际问题无关

如果只为单个组件显示消息,则无需在对话框中使用 <p:messages/> - <p:message/> 也可以正常工作