如何在 Primefaces 中显示从服务器端到对话框的消息

How to show messages from server side to dialog in Primefaces

早上好,我正在做一个方法来验证数据库中是否存在品牌并显示品牌刚刚出现的消息,我想访问对话框“manageBrand”并将此消息放入其中它没有退出对话框。 在 Primefaces 中可以这样做吗?

<p:dialog id ="manageBrand" header="Brand Details" showEffect="fade" modal="true"
                  widgetVar="manageBrandDialog" responsive="true">
                  <p:growl id="messaggi" showDetail="true"/>
            <p:panelGrid id="manage-product-content" class="ui-fluid" columns="2">
                
                   <p:outputPanel rendered="#{not empty marcaController.selected}">
                    <div class="p-field">
                        <p:outputLabel for="name">Name</p:outputLabel>
                        
                        <p:inputText id="name" value="#{marcaController.selected.nome}" required="true"/>
                    </div>
                   
                    </p:outputPanel>  
                  
            </p:panelGrid>

            <f:facet name="footer">
                <p:commandButton value="Save" icon="pi pi-check" action="#{marcaController.creaMarca()}"
                                 update="manage-brand-content" process="manage-brand-content @this"/>
                <p:commandButton value="Cancel" icon="pi pi-times" onclick="PF('manageBrandDialog').hide()"
                                 class="ui-button-secondary"/>
            </f:facet>
        </p:dialog>

public void creaMarca() {
        //m = selectedbrand
       if(m.getIdmarca()==null) {
           List<Marca> list = marcaDao.ricercaMarcaByNome(m.getNome());//find if exixt a brand with selected name
           if(list.isEmpty()||list==null) {
               try {
                marcaDao.create(m);
                FacesMessage m = new FacesMessage
                        (FacesMessage.SEVERITY_INFO, "Creazione andata a buon fine"
                                ,"OK");
                FacesContext.getCurrentInstance().addMessage("Success", m);
            } catch (Exception e) {
                FacesMessage m = new FacesMessage
                        (FacesMessage.SEVERITY_ERROR, e.getMessage()
                                ,"KO");
                FacesContext.getCurrentInstance().addMessage("Error", m);
                
                
            } 
               PrimeFaces.current().executeScript("PF('manageBrand').hide()");
                PrimeFaces.current().ajax().update("form:messages", "form:datatable-brands");
           }
           else {
               
               FacesMessage m = new FacesMessage
                        (FacesMessage.SEVERITY_INFO, "Brand  exist "
                                ,"OK");
               
               PrimeFacesContext.getCurrentInstance().addMessage(null, m);
               PrimeFaces.current().ajax().update(":messaggi","form:manageBrand");
               //PrimeFaces.current().dialog().showMessageDynamic(m);
               
           }
            
       }


在您的情况下,通过 ID 更新对话框将关闭它。为避免这种情况,您应该只更新所需的元素或其容器,或者删除该更新,并将咆哮设置为自动更新。另一种方法应该是将服务器端的消息设置为特定的 id。