Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loading from MessageResources.properties in Struts 2

 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry; forgot we're having a wiki snippet issue.

http://struts.apache.org/2.1.6/docs/localization.html
 
Shashank Rudra
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you dun need to tell struts.xml rather declare this in struts.properties file
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
XML configuration is preferred over the properties file.
 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...

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
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's just saying it *could* be used--not that it *should* :)
 
Shashank Rudra
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic