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

    facesmessages in java code. Problem with the documentation

    aldian00

      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
          monkeyden

          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">
              <navigation>
                  <rule if-outcome="success">
                   <redirect view-id="/some/other/view-id.xhtml"/>
               </rule>
              </navigation>         
          </page>
          



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


          messages.hello=Hello
          



          and have this in the code:


          @Name("hello")
          
          @Stateless
          
          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.


          http://docs.jboss.com/seam/2.1.2/api/org/jboss/seam/faces/FacesMessages.html


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