【问题标题】:primefaces messages not displayedprimefaces 消息未显示
【发布时间】:2013-02-25 06:03:12
【问题描述】:

我得到了一个带有 single_selection 数据表和一个命令按钮的页面。命令按钮调用一个 bean 方法来验证是否进行了选择。如果不是,它应该显示一条消息警告用户。如果进行了选择,它将导航到另一个页面。

问题是没有选择时没有消息显示。如果进行了选择,则可以正常工作。

这是页面的代码:

<h:body>
<h:form id="form">
<p:dataTable id="TablaSociedades" var="soc" value="#{Sesion.tablaSociedades}" emptyMessage="No hay Registros que Mostrar" selection="#{Sesion.sociedadUsuario}">  
  <f:facet name="header">Sociedades asociadas al Usuario</f:facet> 
     <p:column selectionMode="single" style="width:18px" />  
     <p:column>
        <f:facet name="header">
           <h:outputText value="Sociedad" />
        </f:facet>
           <h:outputText value="#{soc.nombreSociedad}" />
     </p:column>
  <f:facet name="footer">  
     <p:commandButton action="#{Sesion.mostrarMenu()}" value="Continuar" process=":form" />  
   </f:facet> 
</p:dataTable>
<br/>
<div>
  <p:messages id="messages2" showDetail="true" autoUpdate="false" closable="true" /> 
</div>
</h:form>
</h:body>

以及bean方法的代码:

  public boolean mostrarMenu()
{
    boolean resp = true;

    if (sociedadUsuario.getNombreSociedad().equals("")) //no selection
    {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage  (FacesMessage.SEVERITY_WARN,"Seleccione una Sociedad", "No puede Continuar"));  
        resp = false;
    }

    return resp;
}

我必须提到命令按钮必须单击两次,但我会针对该问题发布另一个问题。

我做了另一个带有 bean 验证的页面,其中消息正确显示,与此页面的唯一区别是另一个页面仅包含 inputText、commandbutton 和消息。

感谢您的宝贵时间

2013 年 2 月 25 日 faces-config.xml 文件的代码。

<navigation-rule>
   <from-view-id>/elegirSociedad.xhtml</from-view-id>
   <navigation-case>
    <from-action>#{Sesion.mostrarMenu()}</from-action>
    <from-outcome>true</from-outcome>
    <to-view-id>/principal.xhtml</to-view-id>
    <redirect />
   </navigation-case> 
</navigation-rule>

我使用重定向来避免我之前评论但无济于事的“点击两次”问题

【问题讨论】:

    标签: primefaces messages


    【解决方案1】:

    底线是,如果您对两个或多个页面使用相同的托管 bean,请使用 for 属性标识您的消息,并在 bean 方法中创建消息时使用相同的 id。例如:

    <p:messages for="MessageId" showDetail="true" autoUpdate="true" closable="true">
    

    当您在托管 bean 中创建消息时:

    FacesContext.getCurrentInstance().addMessage("MessageId", new FacesMessage  (FacesMessage.SEVERITY_WARN,"Be Warned!", "You did something wrong!"));  
    

    因此,Managed Bean 中的每条消息在页面中都有其对应的组件。

    【讨论】:

      【解决方案2】:

      消息未显示,因为您没有更新它。将update 属性添加到您的p:commandButton

      <p:commandButton action="#{Sesion.mostrarMenu()}" value="Continuar" process=":form" update=":form:messages2"/>
      

      另外,我看不到您在此处的导航方式?

      【讨论】:

      • 我在命令按钮中使用了更新属性。我尝试使用消息 true 和 false 的自动更新属性,但没有显示消息。导航在 faces-config.xml 文件中定义(更新问题中显示的代码)
      • 终于我可以检测到并修复问题了……我在 2 个页面上使用了同一个 bean,并且在两个页面中都显示了不同的消息。当我使用 addMessage 时,我没有使用组件 id 属性,因此消息处于全局模式,不知何故这阻止了消息显示在第二页中。所以我添加了组件 id 属性,消息现在就显示出来了。
      猜你喜欢
      • 1970-01-01
      • 2017-03-03
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 2012-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多