How can I obtain the message resources object in a servlet?
Asked Answered
T

3

7

I'm developing a project with Struts and I was wondering if it's possible to get the message resources object in a servlet, which is included in the same project.

There's no possibility to get that object with the method getResources(HTTPServletRequest) because the servlet does not extends an Action class. Is there a way to do it?

Thanks in advance.

Therapeutics answered 20/10, 2009 at 7:0 Comment(0)
T
8

Well, I finally found how to do it. Just if somebody gets stuck in the same issue, here's the solution: use the java.util.ResourceBundle class in your servlet.

You just have to create the ResourceBundle passing along the name of the properties class and the locale you want to use, like you can see below:

ResourceBundle rb = new ResourceBundle("com.foo.package.theClass", myLocale);
//And then get the messages from the rb object
rb.getMessage("myPropertiesKey");
Therapeutics answered 28/10, 2009 at 7:41 Comment(3)
ResourceBundle is abstract (at least in Java 6). You need to call ResourceBundle.getBundle("com.foo.package.theClass", myLocale) instead of instantiating the class.Tardif
You're absolutely right, Jeremy. It has to be done that way, thanks for pointing that out!!Therapeutics
There is no method getMessage() in class java.util.ResourceBundle, but getString() there is. Thanks a lot for good advice, it really works.Offertory
H
0

You can also do something like this:

ActionContext.getContext().getActionInvocation().getAction() //the action context is threadlocal

Once you have the action, you can use the TextProvider interface to get whatever resource you need for that action.

Headed answered 6/11, 2009 at 6:55 Comment(0)
N
-1

MessageResources-object is stored in the request scope with the key Globals.MESSAGES_KEY ("org.apache.struts.action.MESSAGE").

PropertyMessageResources p = (PropertyMessageResources) request.getAttribute(Globals.MESSAGES_KEY);
String messageValue = null;
if (p != null) {
  // Value for key errors.notempty
  messageValue = p.getMessage("errors.notempty"));
}
Nautch answered 20/10, 2009 at 8:33 Comment(3)
Thanks for your help but I can't make it work. request.getAttribute(Globals.MESSAGES_KEY); returns null. I forgot to say some more info about it. I'm making use of the library Struts 1.3.10 and the servlet is defined in the web.xml as you can see below: <servlet> <description> </description> <display-name> PvsInterface </display-name> <servlet-name>PvsInterface</servlet-name> <servlet-class>com.ford.ads.rapid.pvsw.pvs.PvsInterface</servlet-class> <load-on-startup>1</load-on-startup> </servlet> Am I missing something else? Thanks in advanceTherapeutics
how do you configure your message resources? Does your struts-config.xml contain something like this: <message-resources parameter="StrutsMessages" /> And if you do, is your message-resources properties file in your classpath?Nautch
Thanks for answering. Yes, I have the message resources configured in the struts-config.xml file. And the resources files are where the code is, in a package called resources.Therapeutics

© 2022 - 2024 — McMap. All rights reserved.