Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using two properties files in struts

 
kirans reddy
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Iam using two properties files in struts
iam not using any default properties file in context
For ActionMessage aa=new ActionMessage(key) at first time it loads message resources to cache and after onwards it is retriewing from cache only.

In the two properties file iam using the same key(It is our requirement)

How can mention to action messages to retriew from specific bundle


Hi it is urgent
can any one knows tell me the solution
 
Mudit Srivastava
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could retrieve your message from the ResourceBundle directly.


after this you could slot your key into ActionMessage constructor.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"vas galaxy", please check your private messages for an important administrative matter.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, please read this.
 
kirans reddy
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you for your reply.

From your reply i will get the value of the key and how can i convert the string into ActionMessage(I don't know)


But my requirement is I want to save messages like this.here at first which properties are loaded in cache from cache only the properties are reading.In two properties file iam using the same key with different value(It is my requirement)

ActionMessages messages = new ActionMessages();
ActionMessage message = new ActionMessage("key","arg0");
messages.add(ActionMessages.GLOBAL_MESSAGE, message);
if (!messages.isEmpty()) {
saveMessages( request, messages );
}
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The properties file are loaded into memory depends upon to which properties file you are using first. Say, if you have two properties file, like
1. MessageResoureces_US
2. MessageResoureces_FR

Then, when you load, "MessageResoureces_US" a .property file, then by default it gets loaded into memory(cache), and used for all the consequent "keys" request.
 
kirans reddy
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yaa you told right,
but is there any way to read from MessageResoureces_FR(at first MessageResoureces_US is loaded into cache)

 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kirans reddy wrote:yaa you told right,
but is there any way to read from MessageResoureces_FR(at first MessageResoureces_US is loaded into cache)

Yes, just read that file first, like this,
 
kirans reddy
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Locale currentLocale = new Locale( "fr");
ResourceBundle myMessages = ResourceBundle.getBundle("MessageResource1",
currentLocale);
String key = myMessages.getString( "key");

through this i get key value no problem.
But how can i convert it into ActionMessage

ActionMessages messages=new ActionMessages();

messages.add(Gloabal.key,ActionMessage);

Here it accepts only ActionMessage

 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kirans reddy wrote:through this i get key value no problem.
But how can i convert it into ActionMessage

You need to learn how action messages/errors are added into ActionMessages object.

try this code:
 
kirans reddy
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
new ActionMessage("your.key")

from which properties file it read,iam using two properties file for my application
ApplicationResources1.properties
ApplicationResources2.properties

In both properties file i have same key

your.key=yes-----------ApplicationResources1.properties
your.key=no----------- ApplicationResources2.properties
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kirans reddy wrote:from which properties file it read,iam using two properties file for my application
ApplicationResources1.properties
ApplicationResources2.properties

You can't use the ActionMessages directly, if you are NOT using local specific messages resource files. Now there are two solutions,

1. Either use the above properties file and construct ActionMessages, like


For more info, ActionMessage

2. OR, load a locale specific message resources,
If you have to define files like,
1. MessageResoureces_US
2. MessageResoureces_FR

The using PropertyMessageResources#loadLocale()method, you can get the local specific message key for new ActionMessage(string).
 
kirans reddy
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
# Locale currentLocale = new Locale( "fr");
# ResourceBundle myMessages = ResourceBundle.getBundle("MessageResource", currentLocale);
# String msgText = myMessages.getString( "key");
# ActionMessages messages=new ActionMessages();
# messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(msgText, false));

In this msgText gives value for that key.
new ActionMessage(msgText, false) how it recognize it.
it treat msgText as key and it can not found the key in properties file

And also in my case the two properties file iam not using for locale set.these two are for different clients.
For one client i am using one name and for another with other name with same code
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kirans reddy wrote:# Locale currentLocale = new Locale( "fr");
# ResourceBundle myMessages = ResourceBundle.getBundle("MessageResource", currentLocale);
# String msgText = myMessages.getString( "key");
# ActionMessages messages=new ActionMessages();
# messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(msgText, false));

Use CodeTags
kirans reddy wrote:
In this msgText gives value for that key.
new ActionMessage(msgText, false) how it recognize it.

I provided the JavaDocs link for ActionMeassgae() constructor, that explains your doubt.
kirans reddy wrote:
it treat msgText as key and it can not found the key in properties file

Is that what happening's with you, i.e 'msgText' is coming as a key ?
kirans reddy wrote:
And also in my case the two properties file iam not using for locale set.these two are for different clients.
For one client i am using one name and for another with other name with same code

How its possible to have two different message for the same "key" ? And locale are used for i18n.
 
kirans reddy
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the servletcontext object for the key org.apache.struts.action.MESSAGE
PropertyMessageResources is set as value.

This value set at a time for application which resources are loaded at first i.e ApplicationResources1 or ApplicationResources2

If i change this attribute then working fine but if i change every time it makes performance issue.

So i am searching for alternative one that gets from ApplicationResources1 or ApplicationResources2
which i want to set dynamically and set it to Actionmessage object.




I don't know about this PropertyMessageResources.loadLocale(localeKey) method and also i am new to struts.

for this method PropertyMessageResources.loadLocale(localeKey) localeKey means 'en','zh'
like this or ApplicationResources_en like this.

If it accepting ApplicationResources_en like that my problem will solve

Thanks

 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A simple solution/workaround to your problem. You said, you have two properties file 1 & 2, right and this two file are for respective 2 different client, So can't you load only those property file which belongs to that respective client i.e while releasing your application, you need to set only that property file which belonged to that client and edit the struts-config.xml for loading only that file, like.


kirans reddy wrote:If i change this attribute then working fine but if i change every time it makes performance issue.
And how you measure this performance issue. Look performance is not all about such a little things, if you need to load/switch properties file, then you have to do that. You shouldn't really worried about performance during development phase, lets client reports this problem and the you sit down and using profiler, look at the possible cause of performance.
 
kirans reddy
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok,Thank you
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that's what you're looking for, then you're welcome
 
kirans reddy
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For now its ok i am not considering the performnace issue.If they post i will think of that
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm , apart from that had you given a thought to my firs quote regarding "Different client, port different message resource file, at the time of release".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic