This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes JSF message in faces-config.xml itself Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "JSF message in faces-config.xml itself" Watch "JSF message in faces-config.xml itself" New topic
Author

JSF message in faces-config.xml itself

Ali Manji
Greenhorn

Joined: Sep 01, 2004
Posts: 3
Hi All, JSF Newbie Question:

Is it possible to use messages from a bundle within faces-config.xml?

For example in faces-config.xml I have the following:

<application>
<message-bundle>resources.ApplicationMessages</message-bundle>
</application>

I want to set some default values for a managed bean which contains an array whose values I want to show up in a listBox or comboBox for a JSF Faces page, rather than hard-coding english literals in the faces-config.xml like:

<managed-property>
<property-name>months</property-name>
<list-entries>
<value>January</value>
<value>February</value>
<value>March</value>
<value>April</value>
</list-entries>
</managed-property>

I'd rather reference the message bundle from from the application element type above? Is this possible? If not how does one configure default values through the faces-config.ml file in an international-friendly way?

Thanks in advance
Ali
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

Moving to the Web Franeworks forum where the JSF-savvy hang out.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I think you should be able to do something like:



Where your ApplicationMessages are:

Month1=Janurary
etc

Now, I haven't been able to try this out yet, so I am not 100% sure, but it's worth a shot. I know that you can get a to scoped variable with a method-binding variable. I just don't know if getting to the resource bundle like I suggested is the way to do it.


GenRocket - Experts at Building Test Data
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Well, I just tried what I suggested, although a bit of a simpler test example. And it didn't work. But I really don't know why. I didn't get any error messages about the variable not being there or anything. Hopefully someone else that knows a bit more about this can help us both out.
Ali Manji
Greenhorn

Joined: Sep 01, 2004
Posts: 3
Hi Greg,

Thanks for the quick response. Much appreciated. I tried out your suggestion as well, just to confirm, since it seemed like a good, common-sense, plausible approach.

I hope this is not a flaw or limitation in JSF? If no one else responds, what should the next step be? Should I log a bug with JSF?

Anyone else, any ideas?
Kito Mann
author
Ranch Hand

Joined: Dec 11, 2003
Posts: 115
Originally posted by Ali Manji:
Hi All, JSF Newbie Question:

Is it possible to use messages from a bundle within faces-config.xml?

For example in faces-config.xml I have the following:

<application>
<message-bundle>resources.ApplicationMessages</message-bundle>
</application>

I want to set some default values for a managed bean which contains an array whose values I want to show up in a listBox or comboBox for a JSF Faces page, rather than hard-coding english literals in the faces-config.xml like:

<managed-property>
<property-name>months</property-name>
<list-entries>
<value>January</value>
<value>February</value>
<value>March</value>
<value>April</value>
</list-entries>
</managed-property>

I'd rather reference the message bundle from from the application element type above? Is this possible? If not how does one configure default values through the faces-config.ml file in an international-friendly way?

Thanks in advance
Ali



Ali,

A few things. First, the bundle specified in the <application> node of the configuration file is for JSF messages, not necessarily application strings. It's for FacesMessage instances returned from validators and converters (and concievably your code). So, in that file, you can override any of the standard JSF messages (you just have to use the proper key).

Now, since that file happens to be a property file that's used for localized strings, you can also use it for other localized strings in your application. But the <message-bundle> node in the configuration file doesn't have much to do with that. You can, however, reference strings in that bundle (or any other bundle) in value-binding expressions if you use the <f:loadBundle> tag. So, in your case you would have:



In this snippet, the resources.ApplicationMessage bundle is loaded, and then we reference it in a VB expression using the "bundle" key. Since we can reference "bundle" with any component on the page, it follows that any managed bean can reference it the same way. So, as long as the <f:loadBundle> statement above is on every page that calls your managed bean, you could initialize it like so:




Now, the only problem with this is that it's annoying to have to use <f:loadBundle>. The altenative is to load the resource bundle and expose it as a Map yourself. You could do this by declaring another managed bean that did this work, or you could do it in a ServletContextListener upon application startup.

Hope this helps
.


Kito D. Mann
Author of JSF in Actionwww.JSFCentral.com - JSF FAQ, news, and info
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Kito, the following confuses me..


A few things. First, the bundle specified in the <application> node of the configuration file is for JSF messages, not necessarily application strings. It's for FacesMessage instances returned from validators and converters (and concievably your code). So, in that file, you can override any of the standard JSF messages (you just have to use the proper key).


I thought the point of the bundle specified in the application node was for localization of any strings. What I mean is, if I want to localize text for a label would I not use the bundle loaded in the application node? In your book, this is what you say to do. Or am I getting terms confused here?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: JSF message in faces-config.xml itself
 
Similar Threads
setting parameter in backend bean before JSF page loads
how to over come conversion error
how to navigate JSf form using value from h:selectOneRadio
JSF message in faces-config.xml itself
tomcat 5