Javascript Alert Messages From Message Resource

4 09 2007

alert('<bean:message key="MSG001"/>');

Validation Message:
var fieldname = '<bean:message key="fieldname.amount"/>';
var errorMsg = '<bean:message key=" errors.float" arg0="'+fieldname+'"/>';

In Message Resource:

MSG001 Hello world!
fieldname.amount Payment Amount
errors.float {0} is not a float




6 responses

4 03 2009

The code above merely prints out the literal text that is entered within the alert field. Is there some configuration change you’ve made to allow the alert to retrieve its value from the MessageResource?

4 03 2009

Nope.. don’t remember having to change any configurations

4 03 2009

Looks like adding the = between the key and value in the message resources causes a screw up. Separating them by a space and using it works fine.

4 03 2009

That being said, the nested nature of the errorMsg example seems to be giving issues since the alert is rendering the errorMsg as a literal string instead of extracting both the arg0 and message from the MessageResources.

I am curious as to why the types of quotations used for fieldname is different from the types of quotations used for errorMsg.

4 03 2009

struts syntax uses ” quotations to display a message from the message resource
<bean:message key="MSG001"/>

javascript syntax to alert a string uses ‘ quotations

what we’re doing here is using the struts syntax to get the message we want into the javascript alert. Thats the reason for the different types of quotation

var errorMsg = '<bean:message key=" errors.float" arg0="'+fieldname+'"/>';
the struts requests are always handled first before javascript which is why we can do this

I haven’t touched struts in a long time and am not sure why you’re getting the literal strings but this code worked in my previous project.

Hope I’ve helped a little bit 🙂

18 12 2009
Pablo Cabrera

What if your message has both single and double quotes?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: