【问题标题】:p:messages not displaying inside a dialog in primefacesp:消息未显示在 primefaces 的对话框中
【发布时间】:2017-04-21 14:31:22
【问题描述】:

我在 Primefaces 6 的对话框中有一个 p:messages,但这个元素没有显示任何内容。它只是不显示在屏幕上。我在 bean 中设置错误消息,但它不显示该消息。我希望错误消息显示在对话框中。我尝试在命令按钮和 Bean 中更新 p:messages。代码如下:

豆子:

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>

【问题讨论】:

    标签: jsf primefaces dialog messages display


    【解决方案1】:

    我以前从未见过将“for”属性中的 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);
    

    【讨论】:

    • 刚刚尝试过,但没有成功。该消息仍然没有显示。我添加此 p:messages 的原因是每当我们在数据库事务中遇到异常或任何其他异常时显示错误消息。
    【解决方案2】:

    添加代码中缺少的错误消息后,您需要添加一段 javascript 代码以显示对话框。

    【讨论】:

    • 这只是与问题相关的一段代码。此时,对话框已经显示出来了。
    • 好的。由于 commandbutton 默认触发 ajax 调用,您需要使用 commandbutton 上的 update 属性更新消息组件,以便在 ajax 完成后更新消息。
    • 刚刚尝试过,但没有成功,但理论上我们不应该这样做,因为我们在消息中有 autoUpdate="true":
    • 如果消息确实被添加到支持 bean 中,那么它在页面上的更新问题。您使用 sysout 语句对其进行验证。然后您可以尝试在命令按钮上使用 update="@all"。
    猜你喜欢
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-12
    相关资源
    最近更新 更多