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 "global-messages.properties" 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
password=Password
//.....
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 "struts.properties" file residing in the class path.

//.....
struts.custom.i18n.resources=global-messages
//.........

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.

0 comments:

Post a Comment

Share

Widgets