7 Replies Latest reply on Nov 25, 2008 12:12 AM by spwaters

    Messages and placeHolders

    frer

      Hello,


      I'm trying to figure out how to programmatically (in java code) get the value of a message that has a place holder.


      For example if my messages.properties file contains the following property:


      test=Range between {0} and {1}
      



      How can I get this value from a Seam action.  I saw that there was a FacesMessages class but I don't quite understand it...in the doc I see this snippet:


      @Name("hello")
      @Stateless
      public class HelloBean implements Hello {
          @In FacesMessages facesMessages;
          
          public String sayIt() {
              facesMessages.addFromResourceBundle("Hello");
          }
      }
      



      Yet this will always result in a compilation error since the method sayIt doesn't return anything. 


      I would like to have the equivalent but something like:


      facesMessages.getFromResourceBundle("test", 1, 2);
      



      How can I do this?  I used to use struts and the equivalent in struts was :


      MessageResources.getMessage(String key, Object[] placeHolders)
      



      What is the equivalent in jsf?


      Thank you,


      Francois

        • 1. Re: Messages and placeHolders
          jnusaira

          Are you actually trying to get that message for display in java or display to the HTML page.


          I am not 100% sure how to do it if you want it in your java Code.


          But if you are using it so you can display to the screen all you need to do is this -


          FacesMessages.instance().addFromResourceBundle(FacesMessage.SEVERITY_INFO, summary, params);
          



          That will add the info message to the messages and thus will be displayed on the screen next go around.

          • 2. Re: Messages and placeHolders
            frer

            Hi,


            Thanks for your reply.  In fact it is in Java code that I wish to retrieve the message. 


            I would like to be able to get the message in a String that I can afterwards manipulate.


            Any ideas?


            Thanks,


            Francois

            • 3. Re: Messages and placeHolders
              dan.j.allen
              The FacesMessages component is intended to be used for queuing a JSF FacesMessage instance, possibility from a resource bundle key. However, that's a layer of abstraction over what you need since you are not interested in the FacesMessage in this case.

              What you are looking to do actually involves two steps.

              # Get a message template from a resource bundle
              # Perform positional replacement of parameters in message template

              Assume that you have a message key defined in the English bundle as follows:

              `javax.faces.validator.NOT_IN_RANGE=value must be between {0} and {1}`

              This key can be resolved in a Seam component as follows:

              `String template = ResourceBundle.instance()
                  .getString("javax.faces.validator.NOT_IN_RANGE");
              String resolved = MessageFormat.format(template, 1, 1000);`

              Note that the replacement is handled using a class from the Java API, the same as what JSF uses internally when a FacesMessage is created.

              Seam also supports parameter replacements in a message template, provided by the Interpolator component:

              `String template = ResourceBundle.instance()
                  .getString("javax.faces.validator.NOT_IN_RANGE");
              String resolved = Interpolator.instance().
                  interpolate(template, 1, 1000);`

              Which you use is up to you. The Interpolator has its advantages, but is limited to 10 positional parameters. However, when you use Interpolator, it also replaces EL value expressions.

              Enjoy!

              <span style="font-size: x-small;">Please note that in these examples, you should probably use @In rather than the static instance() methods to get a handle on the built-in Seam components (i.e. ResourceBundle and Interpolator).</span>
              • 4. Re: Messages and placeHolders
                frer

                Thank you very much Dan!  That is exactly what I was looking for.

                • 5. Re: Messages and placeHolders
                  vipseixas

                  Hi Dan.


                  Is there a way to use the interpolator with EL inside a xhtml page?


                  I have a message like page {0} of {1} that I use under some datatables, I made a method that uses the interpolator to return the final message, but it would be much more clean if I can use the interpolator directly in the xhtml page.


                  Be good!

                  • 6. Re: Messages and placeHolders
                    spwaters

                    Hey, I'm not sure if you ever figured this out, but here is how you can do dynamic parameter replacement in an XHTML page:



                    #{interpolator.interpolate(messages['javax.faces.validator.NOT_IN_RANGE'],el.value,el.value2}



                    I think this requires that you have your replacement values expressible via el, but if you do then it works great.  Each parameter is just an el expression without the

                    #{}

                    .


                    I hope this helps you out.


                    -Sean

                    • 7. Re: Messages and placeHolders
                      spwaters

                      I messed up that expression just a bit, should be:


                      #{interpolator.interpolate(messages['javax.faces.validator.NOT_IN_RANGE'],el.value,el.value2)}