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

时间:2015-08-10 13:02:24

标签: jsf jsf-2 primefaces

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

我也有一个相同形式的对话框,用于验证变量是空白还是空,如果是空白,则错误消息应该显示在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."));

            }

}

1 个答案:

答案 0 :(得分:1)

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

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

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

    您拥有null的第一个参数,您应该拥有您希望定位的组件的客户端id

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

  3. 总而言之,你现在应该

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

    哪里&#34; downloadButton&#34;是id

    <p:commandButton/>
    <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/>也可以正常使用

相关问题