How to use Resource Bundles in struts framework

The Resource bundles allow Java applications to be easily internationalized by having application content placed into bundles. This content can then be read by the application at run time. So instead of having content hard coded in the application, the application reads its content from the bundle. One of the benefits of using resource bundles to store application content whether for internationalization or not is that the content can be changed without having to recompile the application. Apart from these, resource bundles can be used as a central repository for content that is common to multiple uses or multiple applications or multiple web resources like JSPs. Having content in a central repository reduces unnecessary duplication.

The Struts framework has built-in support for working with Java’s resource bundle mechanism. Having this support allows the Struts framework to seamlessly support application internationalization as well as have a mechanism for externalizing content so that it can be easily changed without having to modify JSPs or application code. The Struts framework uses resource bundle resources throughout the framework. For example, resource bundle resources can be accessed from JSPs to populate them with content. Similarly, action objects can access content stored in resource bundles to do such things as generate error or informational messages that get displayed on screen. The Struts Form Bean validation mechanism is also tied to resource bundles for managing error messages. Like these, there are several uses for resource bundles throughout Struts framework.

Using resource bundles in Struts is as easy as creating a properties file to store the resources in, and then configuring Struts to use the properties file. Once this is done, accessing the resources is straightforward. Following is a very simple resource bundle properties file containing a few properties:

The default content of resource bundle properties file which comes with struts framework is shown below, these content are used for struts validation framework and other things of struts framework.

Apart from struts validation keys, you can put your own application specific contents.

errors.required= The '{0}' field is mandatory.
errors.minlength='{0}' field can not have less than {1} char.
errors.invalid=The '{0}' field is invalid.
errors.maxlength='{0}' field can not be greater than {1} char.
errors.range='{0}' field is not in the range {1} through {2}.
errors.byte='{0}' field must be a byte.'{0}' field is not a valid date.
errors.double='{0}' field must be a decimal of type double.
errors.float='{0}' field must be a decimal of type float.
errors.integer='{0}' field must be an integer.
errors.long='{0}' field must be a numeric of type long.
errors.short='{0}' field must be a numeric of type short.'{0}' field is not a valid credit card number.'{0}' field is an invalid e-mail address.
errors.url='{0}' field is an invalid url.'{0}' field is not a valid phone no.
registration.firstname=First name
registration.lastname=Last name
registration.gender=Gender no

The Resource bundle properties files simply contain key/value pairs. The resources are accessed by their key. The standard name for the resource bundle properties file in Struts is In order for Struts to be able to load this file, it must be stored in your application’s classpath. For example, it could be stored in the /WEB-INF/classes directory.

The below code snippet configures the resource bundle with Struts:

<message-resources parameter="ApplicationResources" />

The parameter attribute of the message-resources tag specifies the fully qualified name of the resource bundle properties file minus the .properties file extension. In this example, a file named would be stored in the /WEB-INF/classes/ directory.

Once a properties file has been created and configured in the Struts configuration file, the resources in the bundle can be accessed from several places in the Struts framework. The most common place is in JSPs. The following code shows how to use the Bean Tag Library’s message tag to load a message from the resource bundle:

<%@ taglib uri="" prefix="bean" %>
<title><bean:message key="registration.firstname"/></title>

The value specified with the message tag’s key attribute is the key for a message in the resource bundle. At run time of your application, Struts retrieves the message and places it in the JSP.