struts-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "James Mitchell" <>
Subject Re: Message Resources From Database
Date Fri, 02 May 2003 14:38:53 GMT
Sorry for taking so long to respond.

> ----- Original Message -----
> From: "M Nadzaruddin Kahar" <>
> To: <>
> Sent: Thursday, May 01, 2003 7:41 AM
> Subject: Message Resources From Database
> Hey James,
> I found the thread with regards to creating message resources from a
> database. I couldn't get to the sample you've worked on. That's why I am
> emailing you. I tried to email you with that telocity email address. I
> guess you no longer use it.

That's correct.  I switched ISPs recently and so I lost that address.
I use the one now, because I can easily switch where it forwards

> Anyway, I followed the struts documentation in trying to create a custom
> MessageResources from a database. According to the documentation, you need
> to subclass both MessageResources and MessageResourcesFactory. I did that
> and I named both of them DatabaseMessageResources and
> DatabaseMessageResourcesFactory respectively. However, later on I realized
> that method MessageResourcesFactory.createResources only accepts a string.
> Since every time a factory is created it's casted to
> MessageResourcesFactory, no matter what you do the subclass ( add some
> methods to accept servlet context for example ) won't be seen. So all I
> could do was to play around with String config parameter. It was pretty
> much
> useless because I wanna make use the datasource created from
> struts-config.xml. Since the datasource is stored in the servlet context,
> and if I can't pass the servlet context to my DatabaseMessageResources, I
> couldn't do anything. Then I thought perhaps I could simply hard code the
> connection details within DatabaseMessageResources itself. This proved
> pointless because if I do it that way, I won't be using the connection
> pool
> since the connection pool is stored in the servlet context as well. So I
> am
> pretty much stuck.

Yep, that's the way it was implemented, I've done my own grumbling over this
same issue, but it's not going to change before 1.1.

> Later on I found out you've implemented some sort of work around for this.

Sort of.

> So I am kind of curious as to how you managed to do it. I hope you don't
> mind sharing a tip or two. I don't know why the design of MessageResources
> was pretty much limited in perspective. It only looked at retrieving the
> resources from properties files.

I agree!!

> It's not flexible at all. To be honest I am
> not a fan of storing resources in files. It's going to get cumbersome to
> maintain and besides it's difficult to enforce data security on them. If
> we
> put them in a database, it's a lot easier to establish their relationship
> with the rest of the data model of an application.

And that's why I created OJBMessageResources.

The download is available here:

> The framework of a company that I have worked for used cache to store the
> resources. It puts it in the session and it's initialized by the
> controller servlet.

The Struts framework also caches the key-value pairs.  It even goes one step
further, it caches the "formatted" pairs.

So (for example) if you are using:

some.value=Please don't forget to fill in the ${0} field.

...then when you call it, you pass arg0="First Name", then struts only has
to do the substitution one time.  It isn't configurable, but you can grab
internal field and dump the map (as I do) to acheive no-cache.

> I think someone mentioned about using cache as well, which I think
> is definitely a good idea. I don't know a whole lot about fulcrum but I
> must
> have stumbled upon some message in a forum or something that you can
> actually synchronize it with the current data in your database.
> Thanks.
> Naz

Using the OJBMessageResources is quite simple:

Here's what I did today, while writing this response.

I downloaded the struts nightly build from last night.  Then took the
struts-example.war and...

 - renamed to
 - unzipped it
 - moved to tomcat/webapps
 - copied jars to tomcat/webapps/struts-example/WEB-INF/lib
   - jdbc2_0 (you know what I mean)
   - mm.mysql (ditto)
   - ojb-msg-res.jar (from ojb download)
   - jakarta-ojb-0.9.4.jar
   - log4j.jar
 - copied ojb files (from ojb download)
    (you can change these to suit your needs, or use them "as is")
   - repository.dtd
   - repository.xml
   - repository_internal.xml
   - repository_user.xml
 - ran 2 sql scripts against mysql
   - mysql.sql
   - struts-example-data.sql
 - changed message-resource in struts-config to use custom factory


 - started the webapp

James Mitchell
Software Developer/Struts Evangelist

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message