aspose file tools*
The moose likes Struts and the fly likes Loading from MessageResources.properties in Struts 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Loading from MessageResources.properties in Struts 2" Watch "Loading from MessageResources.properties in Struts 2" New topic
Author

Loading from MessageResources.properties in Struts 2

Dave Alvarado
Ranch Hand

Joined: Jul 02, 2008
Posts: 436
Hi,

I'm migrating my Struts 1 application to Struts 2. In my Struts 1 app, I had a MessageResources.properties file in which I stored key-value pairs of error messages. For example,

error.no.redirect.found=No redirect could be found because the database is down.

How can I continue to utilize these properties in Struts 2? In my action class, I have this validate method ...



But right now, the string "actionError" is a key in the properties file. How do I turn it in to the value? Thanks, - Dave
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

http://struts.apache.org/2.x/docs/localization.html
Dave Alvarado
Ranch Hand

Joined: Jul 02, 2008
Posts: 436
Thanks. A follow up to this, what does the properties file need to be called? Currently, I have a MessageResources.properties file in my classpath, but obviously I need to tell struts.xml about the file somehow or rename it to what it is expecting.

Thanks, - Dave
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Sorry; forgot we're having a wiki snippet issue.

http://struts.apache.org/2.1.6/docs/localization.html
Shashank Rudra
Ranch Hand

Joined: Mar 26, 2009
Posts: 131
you dun need to tell struts.xml rather declare this in struts.properties file


Programmer Analyst || J2EE web development/design
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

XML configuration is preferred over the properties file.
Dave Alvarado
Ranch Hand

Joined: Jul 02, 2008
Posts: 436
I added a file "MyAction.properties" in the same directory as the class for that file and everything loaded correctly.

Thanks, - Dave
Shashank Rudra
Ranch Hand

Joined: Mar 26, 2009
Posts: 131
I think the XML way of configuration in not present in Struts2.0.6 version. As when I am trying to declate the property file in struts.xml as I am getting this error Or can it be worked out in 2.0.6 also someway?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

...

In general, randomly trying configuration elements won't work.

http://struts.apache.org/2.1.6/docs/localization.html

Note the struts.custom.i18n.resources property, although XML property configuration is preferred. See this also (FAQ entry).
Shashank Rudra
Ranch Hand

Joined: Mar 26, 2009
Posts: 131
ok. So David when you say XML way of doing it - you mean Listener way of doing it. However will creating an additional class- be not overhead, if the messages are being picked from the struts.properties file using
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I have no idea what you're talking about. There are no listeners or additional classes: just set the constant in the XML config file (or web.xml) instead of in a struts.properties file--just like it says in the XML config link I provided.
Shashank Rudra
Ranch Hand

Joined: Mar 26, 2009
Posts: 131
oh I was following the link attached to
see this also
this ahead of the one I was to see. My mistake. Though what that Listener in there is for? Thanks David for all the help.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That's just saying it *could* be used--not that it *should* :)
Shashank Rudra
Ranch Hand

Joined: Mar 26, 2009
Posts: 131
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Loading from MessageResources.properties in Struts 2