Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

primefaces messages not displayed

I got a page with a single_selection datatable and a commandbutton. The commandbutton invokes a bean method that verifies if a selection was made. If not it should display a message warning the user. If a selection was made it navigates to another page.

The problem is that no message is displayed when there's no selection. If a selection was made it works fine.

Here's the code for the page:

<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>

And the code for the bean method:

  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;
}

I must mention that the commandbutton must be clicked twice but i'll post another question for that problem.

I did another page with bean validation where the messages are displayed correctly, the only difference with this page is that the other page contains only inputText, commandbutton and messages.

Thanks for your time

02/25/13 Code for the faces-config.xml file.

<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>

I used redirect to avoid the "click twice" problem that i comment before but to no avail

like image 231
user2067146 Avatar asked Dec 03 '25 06:12

user2067146


2 Answers

well the bottom line is that if your using the same managed bean for two or more pages, identify your messages with the for attribute and use the same id when you create the message in the bean method. For example:

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

and when you create the message in your managed bean:

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

So each message in your Managed Bean has its corresponding component in a page.

like image 132
user2067146 Avatar answered Dec 05 '25 04:12

user2067146


Message is not displayed because you didn't updated it. Add update attribute to your p:commandButton:

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

Additionally I don't see how you are navigating here?

like image 42
partlov Avatar answered Dec 05 '25 03:12

partlov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!