Saturday, November 8, 2008

Using global messages - struts 2

Putting text resources in properties files is a common web
application development practice enhancing proper management of texts,easy modifications and for internationalization. When it comes to struts 2, it further helps to keep our code clean and reduce the development time. To use global messages with struts 2, we need to create "" and place it on the class path at WEB-INF/classes. All the messages can be put in this file as key- value pairs.

userName=User Name
In order to use this file, we have to tell struts 2 where to find this file. As usual , we need to add one entry into the "" file residing in the class path.


Here, how we use these messages in our view pages. You have to use the "key" attribute of UI components to specify the key from the global-messages.

<s:textfield key="userName"/>
<s:password key="password"/>

Key attribute does infer the name and value for the text filed with message key. Here, proper naming convention is important to have this kind of service from the struts 2 framework.


Post a Comment