2017-04-21 90 views
0

我在Primefaces 6的對話框中有一個p:消息,但是此元素沒有與任何內容一起顯示。它只是不顯示在屏幕上。我在bean中設置錯誤消息,但它不顯示消息。我想要在對話框中顯示錯誤消息。我嘗試更新commandbutton和Bean中的p:messages。下面是代碼:p:在primefaces中對話框中沒有顯示的消息

豆:

public void persist() { 
    boolean error = false; 
    addErrorMessage("schedule_summary_error_message", "FUMBLE! Something went wrong! Try it again.", null); // Added this line so the error message can show no matter what but it still doesn't show. 

    for (int i=0; i < newMatchups.size(); i++) { 
     if (!matchupService.persist(newMatchups.get(i))) 
      error = true; 
    } 

    if (error) { 
     addErrorMessage("schedule_summary_error_message", "Something went wrong! Try it again.", null); 
    } else { 
     reloadCurrentMatchups(); 
     RequestContext.getCurrentInstance().execute("PF('newScheduleDialog').hide()"); 
    } 
} 


private static void addErrorMessage(String clientId, String summary, String detail) { 
    FacesContext context = FacesContext.getCurrentInstance(); 
    context.addMessage(clientId, new FacesMessage(Constant.ERROR, summary, detail)); 
} 

XHTML:

<p:dialog id="newScheduleDialog" appendToBody="true" responsive="true" widgetVar="newScheduleDialog" header="Creating the game schedule" 
position="center top" closeOnEscape="true" width="60%" modal="true" style="overflow-y:auto;max-height:100%"> 
<p:ajax event="close" oncomplete="PF('createScheduleWizard').loadStep(PF('createScheduleWizard').cfg.steps[0], true)" /> 
    <h:form id="schedule_form"> 
     <p:wizard id="createScheduleWizard" widgetVar="createScheduleWizard" showStepStatus="false" flowListener="#{scheduleWizard.onFlowProcess}" showNavBar="false" > 
     <p:tab id="summaryTab" title="Summary"> 
      <p:messages id="schedule_summary_error_message" for="schedule_summary_error_message" showDetail="true" showSummary="false" autoUpdate="true" closable="true" />              
      <p style="text-align: center;" class="sansation_bold16">Summary </p> 

      <ui:include src="../pages/matchup_carousel.xhtml" /> 
      <p:commandButton value="Save and publish schedule" id="saveButton" widgetVar="saveButton" actionListener="#{matchupBean.persist}" 
           style=" -webkit-border-radius: 6px; border-radius: 6px; margin-top:20px"/> 
     </p:tab> 

    </p:wizard> 
</h:form> 

回答

1

我還沒有看到之前具有相同id使用id中 「爲」 屬性元素本身。嘗試在消息的「for」上使用不同的ID(表單的ID也許)。嘗試:

<p:messages id="schedule_summary_error_message" for="schedule_summary" showDetail="true" showSummary="false" autoUpdate="true" closable="true" /> 

,並添加綁定到窗體的ID的消息:

addErrorMessage("schedule_summary", "Something went wrong! Try it again.", null); 
+0

剛剛嘗試過,但沒有奏效。該消息仍然不顯示。我添加這個p:messages的原因是當我們遇到數據庫事務中的異常或任何其他異常時,顯示錯誤消息。 – flacoding

0

您需要添加的JavaScript代碼段添加這是在你的代碼所缺少的錯誤消息後,顯示該對話框。

+0

這只是與問題相關的一段代碼。此時,對話框已經顯示。 – flacoding

+0

好的。由於commandbutton默認觸發ajax調用,因此需要使用commandbutton上的update屬性更新消息組件,以便在ajax完成後更新消息。 – OTM

+0

剛剛嘗試過,但它不起作用,但理論上我們不應該這樣做,因爲我們對消息具有autoUpdate =「true」: flacoding