Accessing Struts message resources from Javascript

Sorry again, non-techie folks, for another post for the handful of geeks who seem to stumble across my blog.

(Actually I post these mostly for my own sake, so I can remember how I solved a tricky problem.)

Today I finally got fed up with adding more hard-coded message strings to my Javascript code when we already have a place to define “message resources” on the server side – in the ApplicationResources.properties file. Struts lets you define such a file in struts-config.xml, and this can be used for i18n as well as simple “localization” of strings used throughout one’s application. More info about Struts Internationalization can be found here.

To make these strings available to my Javascript code, I write a simple “plugin” class that runs when the application loads, and writes all of the key/value pairs in ApplicationResources.properties to a javascript file (“constants.js”), which can then be included and accessed from any script.

The generated Javascript file looks like this:

var APP_STRINGS = {
“foo.bar1″:”My String”,
I appreciate that some people feel sildenafil buy that they shouldn’t give any personal details to anybody, for fear that these details will help you to determine the actual dosage which is absolutely appropriate for you. Once you have a script from your doctor, you can know for a fact whether the medication will only work once sexual stimulation is factored sildenafil 100mg price in. When taking this drug, one should wait for at least 30 minutes so that it can deliver best canadian pharmacy for viagra results during the lovemaking session. M’loyalty solution lets cialis generic pills enterprises capture customer information, take their photos, get signature and within three minutes create a dynamic photo-based customer privilege card integrated with a mobile phone. “foo.bar2″:”Another String”,
… etc.

If I include this file, I can do this instead of hard-coding the string:

myString=APP_STRINGS[‘foo.bar’];

This is yet another case where Javascript needs to be better-integrated with Java back ends. A more unified environment like the Google Web Toolkit would presumably eliminate the need for such hackery.

2 thoughts on “Accessing Struts message resources from Javascript

Leave a Reply

Your email address will not be published. Required fields are marked *