Produce to work with Properties file in Struts Application

Produce to work with properties file (resource bundle file) in our struts application to make presentation logic labels as flexible labels to modify:-

With respect struts first application:-

Step1:- keep properties file in WEB-INF / classes folder of 1st application having presentation labels.

                Myfile.properties

                # Presentation logic

  1.                 My.un = <b> login username </b>
  2.                 My.pass = <b> login password </b>
  3.                 My.btn.cap = <b> check Details </b>
  4.                 My.welcome.msg = <font color = red dize =7> welcome to Struts </font>

Note: – in Struts environment properties file must have properties extecsion.

Step2:- configure the above properties file Struts-config.xml file.

In Struts-config.xml                                                              docs \ dtd docs \

After </action-mapping>

<message-resource parameter = “myfile”/> where myfile is the name of the properties file

Step3:- read & use properties file content as labels in jsp program.(register.jsp)

Register.jsp:-

  1.     <% @ taglib uri = “http://Struts.apache.org / tags-html” prefix =”html” %>
  2. <% @ taglib uri = “http://Struts.apache.org / tags-bean” prefix =”bean” %>
  3.        <bean: message key =”my.welcome.msg”/> <br>
  4.             <html: form action =”register” method = “get”>
  5.                 <bean: message key = “my.un”/><html: text property =”username”/> <br>
  6.             <bean: message key = “my.pass”/> <html: password property =”password”/> <br>
  7.                 <html: submit style =”font-size = 20px”>
  8.                <bean: message key =”my.btn.cap”/>
  9.          </html: submit>
  10.       </html: form>

Note: –   properties file can make presentation logic labels as resource labels for the multiple resource of view layer (jsp programs).

  • When Struts project is created in Netbeans it comes with one default properties file having application resources properties. This file will be configured in Struts-config file automatically.
  • If we want use one message of properties file as multiple presentation logic labels in the jsp program generated web pages having small amount of changes then we can design that message in properties file with arguments  {0}, {1}, {3}, {4}.
  • We can supply these argument values from jsp programs by using arg<n> attribute <bean: message> like arg0, arg1, arg2, arg3, & arg4 attributes.

In properties myfile.properties:-

# Presentation logic

  1.                My.lbl = <b> logic {0} </b>
  2.                My.btn.cap = checkDetails

In register.jsp:-

  1.               <html: form action =”register” method = “get”>
  2.               <bean: message key =”my.lbl” argo = “user”/> <html: text property =”username” />
  3.                                                  <br>
  4.      <bean: message key =”my.lbl” argo = “pwd”/> <html: password property =”password” />
  5.                          <br>
  6.      <html: submit>
  7.                <bean: message key =”my.btn.cap”/>
  8.        </html: submit>
  9.         </html: form>

It is recommended to design properties file having maximum of 500 messages for better performance. If needed it is recommended to take the support of multiple properties file in your Struts application.
When multiple property files are there in Struts application then they need to be configured in Struts-config file with logical names.

Example:-

Step1:- prepare multiple properties files as shown below.

WEB-INF / classes \ my file.properties

       # Presentation logic

  1.                                    My.user = <b> logic user </b>
  2.        My.btn.cap = checkDetails.

WEB-INF /classes \app \pi \App.properties

       # Presentation logic

  1.                                    My.password = <b> logic password </b>
  2.        My.welcome.msg = <font color = red size = 4> welcome to struts </font>.

Step2:- Configure both properties files in Struts configuration file with logical names.

In Struts-config.xml

  1.                  <message-resource parameter = “myfile” key = “f1”/>
  2.                  <message-resource parameter = “app.pi.App” key = “f2”/>

F1, f2-> logical means of properties files configuration.

Step3: – Use <bean: message> along with bundle attribute to read messages from multiple properties files.

 Register.jsp

  1. -<%@
  2. -<%@
  3.                          <bean: message key =”my.welcome.msg” bundle = “f2”/><br>
  4.                             <html: form action =”register” method = “get”>
  5.                             <bean: message key =”my.user” bundle = “f1”/><html: text property = “usermame”/>
  6.                        <bean: message key =”my.pass” bundle = “f2”/><html: password property = “password”/>   
  7.                      <html: submit>
  8.                              <bean: message key =”my.btn.cap” bundle = “f1”/>
  9.                      </html: submit>
  10.                    </html: form>
  • To add the additional properties file to the Struts project IDE

    Right click on source package folder -> new -> other  ->other -> properties file -> next

            Filename: myfile ->finish

Note: – this properties file must be configured in Struts config file explicitly.

  • Org.apache.Struts.util.MessageResource class object can represent one properties file of Struts application being from Struts Action class.

Leave a Reply

Your email address will not be published. Required fields are marked *