Use of {0} is required and Accessing resource bundles programmatically in Struts

By: Manoj Kumar Emailed: 1645 times Printed: 2119 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Between messages “First Name is Required” and “Last Name is Required”, only the field name changes. We can reuse the message as {0} is required. {0} is replaced with the display name for the field at runtime.

a. Add the following to the MessageResources.properties.

error.required={0} is Required

b. Remove the following error messages from MessageResources.properties

error.cust.firstname.empty=First Name is Required
error.cust.lastname.empty=Last Name is Required

c. Change the validate() method in CustomerForm to use the following type of logic for validating each of first name and last name.

MessageResources msgRes = (MessageResources)
request.getAttribute(Globals.MESSAGES_KEY);
if (firstName == null || firstName.trim().equals(""))
{
String firstName = msgRes.getMessage("prompt.customer.firstname");
String[] rplcmntValueArr = { firstName };
ActionError err = new ActionError("error.required",rplcmntValueArr);
errors.add("firstName", err);
}

d. Add similar logic for lastName validation also.

NOTE: An easier way to achieve the same result is as follows:

if (firstName == null || firstName.trim().equals(""))
{
String[] rplcmntValueArr = { “First Name” };
ActionError err = new ActionError("error.required",rplcmntValueArr);
errors.add("firstName", err);

 

}

But with this approach, the display value of the field i.e. “First Name” is hardcoded in the ActionForm code. That is not good. We have the MessageResource.properties to externalize the display names for the field anyway. We can reuse it by accessing the MessageResources programmatically.


Struts Home | All Struts Tutorials | Latest Struts Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Manoj Kumar
Importing Keystore example using keytool in Java
for loop example in Java
Result Sets, Cursors and Transactions in SQL
JSP Tags for SQL to connect to a database
Using the qsort() and bsearch() functions with values - example program in C
Using qsort() and bsearch() with strings - example program in C
scanf and sscanf sample program in C
Use of {0} is required and Accessing resource bundles programmatically in Struts
Character Arrays in C
Pointers to functions in C++
atoi(), itoa() in C++
Ticker sample program in J2ME
DateFormat sample program in Java
Random class sample program in Java - nextGaussian(), nextBoolean(), nextBytes(), nextInt(), nextLong(), nextFloat(), nextDouble()
What Is a Reference in C++?

More Tutorials in Struts
Configuring JDBC DataSources in Struts
Struts Classes
FAQ: Why was reload removed from Struts (since 1.1)?
FAQ: Why are my checkboxes not being set from ON to OFF?
Using JavaScript to submit a form in Struts
How to prepopulate a form in Struts
Simple example of using the requiredif Validator rule in Struts
Chaining actions in Struts
When is the best time to validate input in Struts
What is a Plug-in and how to use Java plug-ins with Struts?
7 Best Practices of Struts
Origin and Architecture of Struts
ActionErrors and ActionError in Struts
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
Handling multiple buttons in HTML Form in Struts

More Latest News
Most Viewed Articles (in Struts )
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
Struts 1 vs Struts 2
Struts and Tiles - Steps to use Struts and Tiles
ActionErrors and ActionError in Struts
Handling Duplicate Form Submissions in Struts
What is Struts? Which Version of Struts to use?
Model 1 Architecture
Configuring JDBC DataSources in Struts
ForwardAction in Struts
Struts-GUI and Struts Console
Installing and configuring Tomcat and Struts
Creating Struts Modules
Using Multiple Message Resource Bundles in Struts
Use ImageButtonBean for Image based Form Submits in Struts
Use of {0} is required and Accessing resource bundles programmatically in Struts
Most Emailed Articles (in Struts)
History of Struts
The directories and files of a Struts application
ActionErrors and ActionError in Struts
Using Checkbox & Radio Tags, html:select, html:options in Struts Forms
What is a Model View Controller (MVC) Model?
Creating the first application using Struts 2
Installing and configuring Tomcat and Struts
Model 1 Architecture
Using Multibox in Struts
ForwardAction in Struts
Using JavaScript to submit a form in Struts
WebWork 2 is now Apache Struts 2
Struts 1 vs Struts 2
Introduction to Struts Architecture
Creating Struts Modules