Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Database Resource Bundles and JSTL  RSS feed

 
Saurabh Pande
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

Anyone know how I can extend ResourceBundle so that I can return key:value pairs picked up from a Database?

I want to be able to do this from JSTL.

Is there an implemention of this (DBResourceBundle?) already available somewhere? ListResourceBundle is just not flexible enough for me. Also, I dont want to have to create a new class for every language. Can't I just pass the Local to one class and have it pick up the releated info from a database?

I guess I could cook up something if I was using scriptlets, but how do I do this using JSTL?

TIA
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"The Blue Daisy",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender
 
Saurabh Pande
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK , name changed.

BTW, continuing with the topic at hand, I found some info at:

http://forum.java.sun.com/thread.jspa?threadID=543805&tstart=120

but the most important question, how to have one resource bundle handle all locales is still unanswered...
 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can write a generic DBResource class which takes a Locale in its constructor, and retrieves the appropriate values from the database.

The problem is passing this resource bundle to JSTL.
You can't use the fmt:setBundle tag as that only lets you specify a basename, and then uses ResourceBundle.getBundle() to find the bundle.

ResourceBundle.getBundle() looks for a class/properties file named with the appropriate Locale. That would require at minimum one class for each locale you were supporting. The class could merely extend your generic one, and call it straight away, but the class must still be present.

I suppose it would be possible to define those classes at runtime, based on the Locales in the database. But that involves invoking the ClassLoader.defineClass() method, that takes a byte[] or the class defined according to the VM spec. Not trivial to do.

The other approach I can think of is to shortcircuit JSTL. Instead of using a fmt:setBundle tag to configure the bundle, write your own tag that does it. (Tag preferred to scriptlet code) You just have to know the inner workings of JSTL a little - refer to the specification.

All it has to do is create an object of type javax.servlet.jsp.jstl.fmt.LocalizationContext, and put it into an attribute called "javax.servlet.jsp.jstl.fmt.localizationContext"


I think that should work ;-)
Take a look at the JSTL specification section 8, specifically 8.11.3

Hope this helps,
evnafets
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!