Jsf 2 无法在xhtml页面上显示faces消息

Jsf 2 无法在xhtml页面上显示faces消息,jsf-2,primefaces,Jsf 2,Primefaces,我无法使用facesMessage显示faces消息 Bean代码: if(condition){ FacesContext fc=FacesContext.getCurrentInstance(); fc.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL,"Rounding Rule Saved","R Saved")); } <p:messages autoUpdate="true" showD

我无法使用facesMessage显示faces消息

Bean代码:

if(condition){
    FacesContext fc=FacesContext.getCurrentInstance();
    fc.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL,"Rounding Rule Saved","R Saved"));
}
<p:messages autoUpdate="true" showDetail="true" globalOnly="true" id="msgId"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0     Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"  
      xmlns:h="http://java.sun.com/jsf/html"      
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      > 
    <ui:composition template="/pages/webEx_Template.xhtml">
        <ui:define name="content">
            <h:head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <title>Rounding Rule Master</title>
                <link rel="stylesheet" type="text/css" href="../css/rounding_rule_master.css" />

            </h:head>
            <h:body>
                <div class="wrapper">
                    <div class="page_wrapper">

                        <div class="page_heading border_bottom">Rounding Rule Master</div>

                        <div class="form_div">
                            <h:form  id="roundingMasterFormId" >
                                <p:messages autoUpdate="true" showDetail="true" globalOnly="true" id="MESSAGEID"/>
                                <div class="form_detail">
                                    <div class="float_right">
                                        <h:commandButton id="creataeBtn" action="#{roundingBean.createRoundingRule}" value="Create   Rounding Rule" class="createRelation"  >
                                            <p:ajax onstart="statusDialog.show();" onsuccess="statusDialog.hide();"/>
                                        </h:commandButton>
                                    </div>
                                    <div class="clear"></div>


                                    <div style="margin:10px 0;">
                                        <table id="roundingMasterTableId" width="100%" cellspacing="1" cellpadding="0" border="0" class="role_detail_section">
                                            <thead>
                                                <tr>
                                                    <th>Rounding Rule Id</th>
                                                    <th>Rounding Rule Name</th>
                                                    <th>Rounding Mode</th>
                                                    <th>Rounding Constant</th>
                                                    <th class="action">Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                            <c:forEach items="#{roundingBean.roundingRuleList}" var="var" >
                                                <tr>    
                                                <h:inputHidden value="#{var.roundingRuleId}" /> 
                                                <td><h:outputText value="#{var.roundingRuleId}" /></td>
                                                <td><h:outputText value="#{var.roundingRuleName}" /></td>
                                                <td><h:outputText value="#{var.roundingMode.roundingModeName}" /></td>
                                                <td><h:outputText value="#{var.roundingIncrement}" /></td>
                                                <td>
                                                <p:commandLink value="Edit" styleClass="edit_icon" action="#{roundingBean.editRoundingRule    (var,true)}" immediate="true" process="@this" onstart="statusDialog.show();" onsuccess="statusDialog.hide();">
                                                </p:commandLink>
                                                <p:commandLink id="deleteId" styleClass="remove_icon" action="#{roundingBean.deleteRoundingRule(var.roundingRuleId)}" immediate="true" update="@form" process="@this" oncomplete="addCSS()" onstart="statusDialog.show();" onsuccess="statusDialog.hide();">
                                                    <p:confirm header="Confirmation" message="Are you sure want to delete rounding rule ?" icon="ui-icon-alert" />Delete
                                                </p:commandLink>
                                                </td>
                                                </tr>
                                            </c:forEach>
                                            </tbody>
                                        </table>
                                    </div>




                                </div>

                                <p:confirmDialog styleClass="confirm" global="true" showEffect="fade" draggable="false" closable="false" resizable="false" width="400">  
                                    <p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes"/>  
                                    <p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no"/>       
                                </p:confirmDialog>
                                <p:dialog styleClass="confirm" modal="true" widgetVar="statusDialog" header="Loading..."
                                          draggable="false" closable="false" resizable="false" width="100"
                                          height="50">
                                    <div align="center">
                                        <p:graphicImage value="../images/loading.gif" width="50"
                                                        height="50" />
                                    </div>
                                </p:dialog>
                            </h:form>
                        </div>
                    </div>
                </div>
            </h:body>
        </ui:define>
    </ui:composition>
</html>
查看代码:

if(condition){
    FacesContext fc=FacesContext.getCurrentInstance();
    fc.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL,"Rounding Rule Saved","R Saved"));
}
<p:messages autoUpdate="true" showDetail="true" globalOnly="true" id="msgId"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0     Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"  
      xmlns:h="http://java.sun.com/jsf/html"      
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      > 
    <ui:composition template="/pages/webEx_Template.xhtml">
        <ui:define name="content">
            <h:head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <title>Rounding Rule Master</title>
                <link rel="stylesheet" type="text/css" href="../css/rounding_rule_master.css" />

            </h:head>
            <h:body>
                <div class="wrapper">
                    <div class="page_wrapper">

                        <div class="page_heading border_bottom">Rounding Rule Master</div>

                        <div class="form_div">
                            <h:form  id="roundingMasterFormId" >
                                <p:messages autoUpdate="true" showDetail="true" globalOnly="true" id="MESSAGEID"/>
                                <div class="form_detail">
                                    <div class="float_right">
                                        <h:commandButton id="creataeBtn" action="#{roundingBean.createRoundingRule}" value="Create   Rounding Rule" class="createRelation"  >
                                            <p:ajax onstart="statusDialog.show();" onsuccess="statusDialog.hide();"/>
                                        </h:commandButton>
                                    </div>
                                    <div class="clear"></div>


                                    <div style="margin:10px 0;">
                                        <table id="roundingMasterTableId" width="100%" cellspacing="1" cellpadding="0" border="0" class="role_detail_section">
                                            <thead>
                                                <tr>
                                                    <th>Rounding Rule Id</th>
                                                    <th>Rounding Rule Name</th>
                                                    <th>Rounding Mode</th>
                                                    <th>Rounding Constant</th>
                                                    <th class="action">Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                            <c:forEach items="#{roundingBean.roundingRuleList}" var="var" >
                                                <tr>    
                                                <h:inputHidden value="#{var.roundingRuleId}" /> 
                                                <td><h:outputText value="#{var.roundingRuleId}" /></td>
                                                <td><h:outputText value="#{var.roundingRuleName}" /></td>
                                                <td><h:outputText value="#{var.roundingMode.roundingModeName}" /></td>
                                                <td><h:outputText value="#{var.roundingIncrement}" /></td>
                                                <td>
                                                <p:commandLink value="Edit" styleClass="edit_icon" action="#{roundingBean.editRoundingRule    (var,true)}" immediate="true" process="@this" onstart="statusDialog.show();" onsuccess="statusDialog.hide();">
                                                </p:commandLink>
                                                <p:commandLink id="deleteId" styleClass="remove_icon" action="#{roundingBean.deleteRoundingRule(var.roundingRuleId)}" immediate="true" update="@form" process="@this" oncomplete="addCSS()" onstart="statusDialog.show();" onsuccess="statusDialog.hide();">
                                                    <p:confirm header="Confirmation" message="Are you sure want to delete rounding rule ?" icon="ui-icon-alert" />Delete
                                                </p:commandLink>
                                                </td>
                                                </tr>
                                            </c:forEach>
                                            </tbody>
                                        </table>
                                    </div>




                                </div>

                                <p:confirmDialog styleClass="confirm" global="true" showEffect="fade" draggable="false" closable="false" resizable="false" width="400">  
                                    <p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes"/>  
                                    <p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no"/>       
                                </p:confirmDialog>
                                <p:dialog styleClass="confirm" modal="true" widgetVar="statusDialog" header="Loading..."
                                          draggable="false" closable="false" resizable="false" width="100"
                                          height="50">
                                    <div align="center">
                                        <p:graphicImage value="../images/loading.gif" width="50"
                                                        height="50" />
                                    </div>
                                </p:dialog>
                            </h:form>
                        </div>
                    </div>
                </div>
            </h:body>
        </ui:define>
    </ui:composition>
</html>

但我没有收到信息,我做错了什么

编辑:

我的项目流程是:

  • 用户单击查看页面上的编辑按钮
  • 重定向到编辑页面
  • 提交后返回查看页面
  • 渲染消息(我没有收到)
  • 查看代码(rouning\u rule\u master.xhtml):

    if(condition){
        FacesContext fc=FacesContext.getCurrentInstance();
        fc.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL,"Rounding Rule Saved","R Saved"));
    }
    
    <p:messages autoUpdate="true" showDetail="true" globalOnly="true" id="msgId"/>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0     Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"  
          xmlns:h="http://java.sun.com/jsf/html"      
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:ui="http://java.sun.com/jsf/facelets"
          xmlns:p="http://primefaces.org/ui"
          xmlns:c="http://java.sun.com/jsp/jstl/core"
          > 
        <ui:composition template="/pages/webEx_Template.xhtml">
            <ui:define name="content">
                <h:head>
                    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                    <title>Rounding Rule Master</title>
                    <link rel="stylesheet" type="text/css" href="../css/rounding_rule_master.css" />
    
                </h:head>
                <h:body>
                    <div class="wrapper">
                        <div class="page_wrapper">
    
                            <div class="page_heading border_bottom">Rounding Rule Master</div>
    
                            <div class="form_div">
                                <h:form  id="roundingMasterFormId" >
                                    <p:messages autoUpdate="true" showDetail="true" globalOnly="true" id="MESSAGEID"/>
                                    <div class="form_detail">
                                        <div class="float_right">
                                            <h:commandButton id="creataeBtn" action="#{roundingBean.createRoundingRule}" value="Create   Rounding Rule" class="createRelation"  >
                                                <p:ajax onstart="statusDialog.show();" onsuccess="statusDialog.hide();"/>
                                            </h:commandButton>
                                        </div>
                                        <div class="clear"></div>
    
    
                                        <div style="margin:10px 0;">
                                            <table id="roundingMasterTableId" width="100%" cellspacing="1" cellpadding="0" border="0" class="role_detail_section">
                                                <thead>
                                                    <tr>
                                                        <th>Rounding Rule Id</th>
                                                        <th>Rounding Rule Name</th>
                                                        <th>Rounding Mode</th>
                                                        <th>Rounding Constant</th>
                                                        <th class="action">Action</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                <c:forEach items="#{roundingBean.roundingRuleList}" var="var" >
                                                    <tr>    
                                                    <h:inputHidden value="#{var.roundingRuleId}" /> 
                                                    <td><h:outputText value="#{var.roundingRuleId}" /></td>
                                                    <td><h:outputText value="#{var.roundingRuleName}" /></td>
                                                    <td><h:outputText value="#{var.roundingMode.roundingModeName}" /></td>
                                                    <td><h:outputText value="#{var.roundingIncrement}" /></td>
                                                    <td>
                                                    <p:commandLink value="Edit" styleClass="edit_icon" action="#{roundingBean.editRoundingRule    (var,true)}" immediate="true" process="@this" onstart="statusDialog.show();" onsuccess="statusDialog.hide();">
                                                    </p:commandLink>
                                                    <p:commandLink id="deleteId" styleClass="remove_icon" action="#{roundingBean.deleteRoundingRule(var.roundingRuleId)}" immediate="true" update="@form" process="@this" oncomplete="addCSS()" onstart="statusDialog.show();" onsuccess="statusDialog.hide();">
                                                        <p:confirm header="Confirmation" message="Are you sure want to delete rounding rule ?" icon="ui-icon-alert" />Delete
                                                    </p:commandLink>
                                                    </td>
                                                    </tr>
                                                </c:forEach>
                                                </tbody>
                                            </table>
                                        </div>
    
    
    
    
                                    </div>
    
                                    <p:confirmDialog styleClass="confirm" global="true" showEffect="fade" draggable="false" closable="false" resizable="false" width="400">  
                                        <p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes"/>  
                                        <p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no"/>       
                                    </p:confirmDialog>
                                    <p:dialog styleClass="confirm" modal="true" widgetVar="statusDialog" header="Loading..."
                                              draggable="false" closable="false" resizable="false" width="100"
                                              height="50">
                                        <div align="center">
                                            <p:graphicImage value="../images/loading.gif" width="50"
                                                            height="50" />
                                        </div>
                                    </p:dialog>
                                </h:form>
                            </div>
                        </div>
                    </div>
                </h:body>
            </ui:define>
        </ui:composition>
    </html>
    
    
    舍入规则母版
    舍入规则母版
    舍入规则Id
    舍入规则名称
    舍入模式
    舍入常数
    行动
    删除
    
    我不确定这是否是你的情况。但是,如果在生成消息后重定向,它们将在重定向过程中丢失。您需要确保在页面流中的faces配置或任何其他方式中没有重定向。

    您的程序是否正在输入
    if
    语句?试着调试你的程序。你的代码是错误的(我在一个程序中也做了同样的事情,它正在工作)。是的,它在if语句中…和我在其他模块中使用的方法一样,在那里它工作正常,但在当前页面中不工作。它取决于哪个因素。我相信这种行为的原因是在你的代码中的其他地方。你能给我们提供更多的信息吗?我猜是第二个(不知道的)ajax请求阻止了消息的显示。我已经发布了我的查看页面,它可以正常工作了…谢谢…我从2天起就陷入了这个问题。现在我陷入了另一个问题:我从nevigation中删除以获取FacesMessage,但现在我的url没有改变,因为我从一个页面切换到另一个页面。如果我在任何一页上按f5,它会将我推到上一页