override standard ResourceBundle with own implementation in JSTL

Discussions

Web tier: servlets, JSP, Web frameworks: override standard ResourceBundle with own implementation in JSTL

  1. Hello,
    I'm sure (or at least hope) someone had the same problem as the one I'm facing. I wanted to override standard behaviour of ResourceBundle and instead of fetching e.g. properties files from classpath (file system) fetching from database storage. To implement this is not very difficult. But.. here comes the tricky part:

    I want JSTL <fmt:bundle .. ( and <fmt:message ..) to use my ResourceBundle instead of standard java resourcebundle.

    I have been looking all over the place for a solution but nothing seems to work. You could easily make your own tag library or use something other than JSTL, but I want to use the standard!

    Please help :-)

    Gösta
  2. hi,

    check this the tag setBundle if it meets your requirements

    http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/fmt/setBundle.html

    Bhagvan K
    http://www.architectcorner.com
  3. Thanks, but this is basically the same as as <fmt:bundle ..>
    Difference is that if I use
    <fmt:setBundle > the bundle can be used within the requested scope and
    <fmt:bundle ..> within start and endtag.

    My dream <fmt:setBundle > would be with an extra attribute called e.g. "value" that would be the name of a scoped variable that is my own ResourceBundle implementation..

    But I have given up for now..
  4. Solution of problem[ Go to top ]

    I found a rather easy solution to the problem:

    For every session just add
    HttpSession session = .. [get hold of the session]
    ResourceBundle bundle = new PropertyResourceBundle(toInputStream(myOwnProperties)) [toInputStream just stores the properties into an inputstream]
    Locale locale = .. [get hold of the locale]

    javax.servlet.jsp.jstl.core.Config.set(session ,Config.FMT_LOCALIZATION_CONTEXT, new LocalizationContext(bundle ,locale));

    or request if you like..

    javax.servlet.jsp.jstl.core.Config.set(request ,Config.FMT_LOCALIZATION_CONTEXT, new LocalizationContext(bundle ,locale));

    Then you can store your own resourcebundle (i.e. myOwnProperties above) in e.g. a database