Skip to main content

Load Message Resources from Struts Action

Posted by ahashim on March 10, 2007 at 4:39 AM PST

Sometime, you will need to read some text from the resource files in your application from the Struts Action class. The easier way which most developers are using is to open the resource file using the java.util.ResourceBundle class, which will open the file and read its content.

PropertyResourceBundle properties=(PropertyResourceBundle)ResourceBundle.getBundle(propertyFileName);

This will open the file for every request to your action! I am sure that it is very bad from the performance point.

Struts provide a methods in the Action class to get the locale and the resources

getLocale(request) and getResources(request)

For some reason, getResources() didn’t work with me.

I didn’t think about the first solution because I am sure that the resources are loaded in the application and must be stored somewhere, so, lets use. And here is the code

MessageResources resources = (MessageResources) getServlet().getServletContext().getAttribute(bundle);
message = resources.getMessage(locale, key);

I hope that this can help somebody to tune the struts web application performance.

Mirror:

http://egjug.org/hashimblog/2007/03/07/load-message-resources-from-struts-action/

Related Topics >>