1 Reply Latest reply on May 19, 2010 4:51 AM by nbelaevski

    How to show FacesMessage by JavaScript ?

    marioosh.net

      I want to do some exception handling (infos, errors, warnings... etc) by using Faces Messages.

      I have test page:

       

      <h:form>
         <a4j:commandLink id="myaction" action="#{testBean.message}" value="make message" reRender="m" /> 
         <!-- <h:message id="m" for="myaction" /> -->
         <rich:message id="m" for="myaction" />
       </h:form>
      

       

      and managed bean:

       

      public String message() { 
       FacesContext context = FacesContext.getCurrentInstance();
       FacesMessage message = new FacesMessage("My error message");
       message.setSeverity(FacesMessage.SEVERITY_ERROR);
       context.addMessage(FacesMessage.FACES_MESSAGES, message);
       return null;
       }
       
      

       

      ..but it doesn't show message in  h:message and rich:message when i click commandLink

      I stuck with that.

       

      Finally i need to show my FacesMessage in js function for example in alert(message)...

       

      Anybody have simple working example to do that ?

      Thanks in advance and sorry for my poor english

       

       

        • 1. Re: How to show FacesMessage by JavaScript ?
          nbelaevski

          Hi,

           

          In this line: "context.addMessage(FacesMessage.FACES_MESSAGES, message);" instead of passing FacesMessage.FACES_MESSAGES you should pass clientId of a4j:commandLink (check method JavaDoc for more).

           

          There is "oncomplete" attribute that contains JS code. This code is executed after Ajax request completion, so you can add the code that checks messages in FacesContext and show alerts for them.