how to modify <s:textfield> to fetch a value from Applicationresources.properties file

2.2k Views Asked by At

I am developing a simple GUI in which i want username value to be fetched from ApplicationResources.properties file. My file contains following two lines :

label.username= Username
label.password=Password

I am using JSP containing struts 2 tag contains following snippet :

<s:textfield cssClass="login-inp" name="username" key="label.username" size="20" />

But when i run my project i see label.username that indicates value from properties file is not read.. am i missing something ? How to solve this problem ? Kindly help

thanks in advance

2

There are 2 best solutions below

2
On

I am not sure where and how you have defined the properties file but when searching for resource bundles struts2 search them in a specific way

  1. ActionClass.properties.
  2. BaseClass.properties (all the way to Object.properties)
  3. Interface.properties (every interface and sub-interface)
  4. ModelDriven's model (if implements ModelDriven), for the model object repeat from 1
  5. package.properties (of the directory where class is located and every parent directory all the way to the root directory)
  6. search up the i18n message key hierarchy itself
  7. global resource properties (webwork.custom.i18n.resources) defined in webwork.properties

refer document for details /internationalization.

Additionally The Struts 2 key attribute can be used in the textfield tag to instruct the framework what value to use for the textfield's name and label attributes.Instead of providing those attributes and their values directly, you can just use the key attribute.

So either use name="username" key="label.username".

Just go through the official documents for details how this work and how framework search for the property files message-resource-files

0
On

Add the below line in your struts.xml file just on top of the first package defintion.

<constant name="struts.custom.i18n.resources" value="ApplicationResources" />

OR

Add struts.properties file in your src folder. And keep the below property in it.

struts.custom.i18n.resources=ApplicationResources