Friday, March 30, 2007

 

Struts and messageResources.properties

Collecting all of your strings in one file is a good idea (as long as they're sorted somehow -- otherwise it just becomes an incomprehensible jumble). Struts has a very smart way to do this. (That's right, I can admit when something is smart) messageResources.properties and <bean:message /> You can pass in arguments that round out the message and make it more dynamic-y. This is a good thing.

I couldn't figure out how to get Javascript variables in there until Kevin, a sneaky-smart programmer who, like me has a background in C++, and who learned JSPs way faster than I did, pointed out a way to swap them in there after calling the bean:message.

In the messageResources.properties entry, stick an @2bReplaced in there where 2bReplaced is a meaningful variable name. So like this:

errors.badDate=The date @Date is bad.

Then in Javascript, you would do this:

var date = "02/25/-2007";//or something equally inane
var badDateMessage = "<bean:message key='errors.badDate' />"
badDateMessage = badDateMessage.replace(/@Date/g, date);

And then use the error message how you normally would.

So thanks, Kevin, for chucking that piece of functionality my way and making me and the UE people feel better about ourselves.





powered by performancing firefox

Labels:


Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?