7 Replies Latest reply on Jun 27, 2011 6:38 AM by vtyro

    FacesMessage added during render phase not displayed

    sw_bpel

      hello,


      actually i have some problems with FacesMessages. I have a stateful session bean in the conversation scope:


      @Scope(ScopeType.CONVERSATION)



      This Bean has a factory method which loads a list of entities from database on initialization. This works fine if the list isn't null or empty. But when it's empty i want to display a message on the page where normally (positive case) the table with entity-list infos is displayed. Therefor i have the


      <h:messages />



      tag placed on the top of the page. In the factory method i add the FacesMessage like this:


      FacesMessages.instance().addFromResourceBundle( Severity.INFO, "not.found", "entity" );



      but the message is never displayed.
      Is there any way / workaround to display the Messages?


      Greetz, Stefan

        • 1. Re: FacesMessage added during render phase not displayed
          niox.nikospara.yahoo.com

          Hi,


          Are the messages generated in an AJAX request? If so make sure the <h:messages /> is re-rendered.

          • 2. Re: FacesMessage added during render phase not displayed
            sw_bpel

            No, i don't use a AJAX request here. It's only a normal XHTML page without AJAX components.
            I have two cases here. The first case is, that on page load the table with entities is displayed. The second case is, that there are no entities, then i want to display a facesMessage, but it isn't displayed. So there is no redirect or anything else.


            page code:



            <h:messages />
            <a:outputPanel id="tablePanel" rendered="#{entityList != null and entityList.rowCount > 0}">
            <h:dataTable id="entityTable" value="#{entityList}" var="en" rendered="#{entityList.rowCount > 0}">
            ...
            </h:dataTable>
            </a:outputPanel>





            the


            <a:outputPanel />



            Tag i only use here to check if the table should be displayed or not.

            • 3. Re: FacesMessage added during render phase not displayed
              niox.nikospara.yahoo.com

              Have you tried to add the message according to the standard way:


              FacesContext.getCurrentInstance().addMessage(
                null,
                new FacesMessage(FacesMessage.SEVERITY_WARN,"xxxx","yyyy")
              );
              



              Does it display messages added this way?

              • 4. Re: FacesMessage added during render phase not displayed
                lucas84

                I guess that factory method is invoked during render response, after messages part has been rendered.


                <h:messages/>
                ...
                ...
                <rich:dataTable value=#{list}>
                
                ...
                </rich>
                
                



                Does yours xhtml looks like above?? When page is rendered, it first renders h:messages, at this point no messages were created so there is nothing to renderer. Later it renders rich:dataTable and starts resolving list, which finally calls your factory methods (which generates messages). But it's too late for adding messages as h:messages has been already rendered. You should look for log in console, something like this: message was queued but not displayed. If this is an issue, the solution is to call factory method before render response, ie. in page.xml using action execute.

                • 5. Re: FacesMessage added during render phase not displayed
                  sw_bpel

                  Hey Nicos,


                  Have tried this also, but doesn't work.
                  I guess the suggested solution of Lukasz could probably help me. I'm going to try this and post the results. Thanks for your help!


                  Greetz Stefan

                  • 6. Re: FacesMessage added during render phase not displayed
                    sw_bpel

                    Hello Lukasz,


                    yes that's it! I added this to my page.xml


                    <page view-id="/entity/list.xhtml">
                      <action execute="#{EntityListManager.initEntityList()}" />
                      ...
                    </page>



                    and it works now. But is there no other way to publish FacesMessages during the render phase? Or can i turn the rendering of h:messages and rich:dataTable/h:dataTable? Or is there any programmatic solution for this? Can i call/do the rendering of m:messages myself e.g. in my factory method?


                    Anyway, so far thank you very much!

                    • 7. Re: FacesMessage added during render phase not displayed
                      vtyro

                          public static void test() {
                              FacesMessages.instance().addFromResourceBundle(WARN, "message.test");
                              refreshMessages();
                          }

                          private static void refreshMessages() {
                              // if render response phase try add messages to faces context
                              if(FacesContext.getCurrentInstance().getRenderResponse()) {
                                  try {
                                      // localization messages
                                      FacesMessages.instance().afterPhase();
                                      // Seam messages to standart JSF messages
                                      FacesMessages.instance().beforeRenderResponse();
                                  } catch (Throwable t) {
                                      log.error(t, t);
                                  }
                              }
                          }

                      Try this code...