How can I get a message resource object in a servlet?

I am developing a project with Struts, and I was wondering if it is possible to get a message resource object in a servlet that is included in the same project.

There is no way to get this object using the method getResources(HTTPServletRequest)because the servlet does not extend the Action class. Is there any way to do this?

Thanks in advance.

+5
source share
3 answers

Well, finally I found how to do it. Simply, if someone is stuck in the same problem, here is the solution: use the java.util.ResourceBundle class in your servlet.

ResourceBundle, , , :

ResourceBundle rb = new ResourceBundle("com.foo.package.theClass", myLocale);
//And then get the messages from the rb object
rb.getMessage("myPropertiesKey");
+6

- :

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

, TextProvider , .

0

The MessageResources object is stored in the request area using the Globals.MESSAGES_KEY 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"));
}
-1
source

All Articles