-
1. Re: Messages and placeHolders
jnusaira May 23, 2008 11:44 PM (in response to frer)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 May 24, 2008 12:00 AM (in response to 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 May 24, 2008 1:56 PM (in response to frer)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 May 24, 2008 4:33 PM (in response to frer)Thank you very much Dan! That is exactly what I was looking for.
-
5. Re: Messages and placeHolders
vipseixas Jul 17, 2008 4:39 PM (in response to frer)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 Nov 25, 2008 12:10 AM (in response to frer)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 Nov 25, 2008 12:12 AM (in response to frer)I messed up that expression just a bit, should be:
#{interpolator.interpolate(messages['javax.faces.validator.NOT_IN_RANGE'],el.value,el.value2)}