• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading the message resources from a form class

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Accessing the message resources is simple from a JSP using the supplied tags, and accessing them is just as simple from an action class by calling getResources(). However, I need to access the message resources from a form class in one of my validation methods.

One of the ActionMessage constructors takes two parameters, a string (key into the message resources for the error message), and an Object used to fill in the first parameter of the message {0}.

My problem is that the value I want to send in as the second parameter to the ActionMessage constructor is something that I want to read from the action messages.

So far, I have not found an easy way to access these resources other than setting up a resource bundle in the form, which I don't like. Is there a way to get to the MessageResources from a form class?

Thanks.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The resource bundle is stored in Application scope. Fortunately, the ActionForm class that you are extending has a getServlet() method that will allow you to get a reference to the servlet that created the form bean, and hence to the servletContext object. You can therefore get the resource bundle with the following code:

MessageResources messageResources = (MessageResources)super.getServlet().getServletContext().getAttribute(Globals.MESSAGES_KEY));
[ January 30, 2006: Message edited by: Merrill Higginson ]
 
Jack Summers
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Merrill,

That worked perfectly. Thank you so much!
reply
    Bookmark Topic Watch Topic
  • New Topic