1 Reply Latest reply on Feb 2, 2011 12:05 PM by Monkey Den

    facesmessages in java code. Problem with the documentation

    Gab Aldian Newbie

      Hi everybody

      I have a problem with this paragraph from the documentation : Faces messages

      There is not much explanations on how it works. Furthermore, there is obviously a problem, since there is no return statement for the sayIt() method, and the return type of the method facesMessages.addFromResourceBundle(Hello); is void.

      All in all I still don't understand how to make call on the resource bundle from the java code.

      Any explanation would be welcome.

        • 1. Re: facesmessages in java code. Problem with the documentation
          Monkey Den Master

          You're correct, the method should be returning some string, or null.  Presumably, that return value is a string representing the JSF navigation rule, represented by success in the example below:

          <page view-id="/some/view-id.xhtml">
                  <rule if-outcome="success">
                   <redirect view-id="/some/other/view-id.xhtml"/>

          'Hello' in that case is a resource bundle key.  All you need to do is add the message to your resource bundle, for example:


          and have this in the code:

          public class HelloBean implements Hello {
              @In FacesMessages facesMessages;  //inject component
              public String sayIt() {
                  facesMessages.addFromResourceBundle("messages.hello"); //add msg to component
                  return "success"; //or null, to return to the same view id

          then, in your view, you have an commandButton/link which has an action of hello.sayIt

          <h:commandButton id="sayItButton" value="Say It" action="#{hello.sayIt}" />

          Once you have that working, explore the FacesMessages api for different ways to add the message, including Severity, message parameters, etc.


          Note that many FacesMessages methods have been deprecated in favor of StatusMessages, which are functionally similar.