ActionErrors and ActionError in Struts

By: Abinaya Emailed: 1677 times Printed: 2157 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

Struts request handling lifecycle as a happy day scenario is – from the point the user submits an html form till the user sees the next page. In reality, users of your web application may submit incorrect data or sometimes no data at all. You have to catch these as close to the user interface as possible, rather than waiting for the middle tier or the database to tell you that a column cannot be inserted in the database because it was expecting a non-null value. There are two consequences of such programming practice.

  1. Server time and resources are precious since they are shared. Spending too much of server’s time and resources on a request, that we know is going to fail eventually is a waste of server resources.
  2. It has a negative impact on the code quality. Since one has to prepare for the possibility of having null data, appropriate checks have to be put (or NumberFormatExceptions have to be caught) everywhere in the code.

Generally business logic is the toughest code of the system and contains enough if-else blocks as such. More if-else blocks for null checks can only mean two things – bad code and maintenance nightmare. Not an elegant programming to say the least. If only you could verify the validity of the user data as close to the user, then the rest of the code only has to deal with business logic and not invalid data.

//validate() method in the CustomerForm
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request)
{
// Perform validator framework validations
ActionErrors errors = super.validate(mapping, request);
// Only need crossfield validations here
if (parent == null) {
errors.add(GLOBAL_ERROR,
new ActionError("error.custform"));
}
if (firstName == null) {
errors.add("firstName",
new ActionError("error.firstName.null"));
}
return errors;
}

Struts provides validate() method in the ActionForm to deal with user input validations. Let us now look at how you can validate the user input and report errors to the framework. 

The validate() method is called after the ActionForm instance is populated with the form data. A sample validate() method is shown above. In the validate() method, you will notice an object called
ActionErrors is instantiated. All error checks are performed with the usual if-else blocks. If there are errors, then an individual ActionError object is created for the culprit field and added to the ActionErrors. Think of ActionErrors as a Map for the individual ActionError objects. You can associate one or more ActionError objects for each key. The form field name is generally chosen as the key and can have multiple ActionError objects associated with it. The ActionError is either specific to a field in the ActionForm or it is global to the entire form. When the error is specific to a form field, the field name is used as the key in the ActionErrors. When the error is global to the form, the key name is always GLOBAL_ERRORS. Both of the cases are shown in the Listing above.

You might also notice that the ActionError constructor takes a rather cryptic key as the argument. This key is declared in a properties file whose value is the actual error message. The properties file is selected based on the user chosen Locale. The technical term for this properties file where the messages are externalized is Message Resource Bundle. It is based on the Java’s concept of Localization using the java.util.ResourceBundle and has a whole lot of bells and whistles. The properties file also serves another purpose apart from Localization. It lets you change the messages without recompiling the code, and is quite handy while maintaining the code. An entry in the Message Resource Bundle properties file looks like:

error.firstName.null=First Name cannot be null

The RequestProcessor stops any further processing when it gets the ActionErrors object with ActionError objects. The Action instance never gets the control (and never gets a chance to return ActionForward). Hence the RequestProcessor consults the ActionMapping object to find the page to be displayed. Notice that the ActionMapping has an attribute named “input”. This attribute specifies the physical page to which the request has to be forwarded on error. Generally this page is the original page where user entered the data since it is natural that user would want to reenter the data in the same page on error and resubmit.


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(6)


1. View Comment

the default implementation of validate returns null so

adding to errors after a call to super.validate(mapping, request);

causes a NullPointerException


View Tutorial          By: Jerry at 2009-02-01 11:12:06
2. View Comment

why do't you create ActionErrors object like this
ActionErrors errors=new ActionErrors();?

what is the benefit of creating like this
ActionErrors errors = super.validate(mapping, request); ?


View Tutorial          By: veera reddy at 2011-07-15 12:14:57
3. View Comment

@veera reddy,
the use of creating actionerrors from super class validate() method has to have some application level validations which will be applied to all of your action forms. Sometimes, we will validate some properties in super classes and some properties in sub class action forms. This is pretty useful in those scenarios.


View Tutorial          By: San at 2011-08-28 23:14:39
4. View Comment

thanks for posting

View Tutorial          By: sriram at 2011-11-24 06:38:42
5. View Comment

Please provide me a particular flow of validations to be performed on a textbox property in struts 1.3??

View Tutorial          By: Deepali at 2013-05-10 10:38:18
6. View Comment

In the case of Basic validation is it must to specify
<action .......
validate="true"
scope="request"/> please explain the validate() method flow.


View Tutorial          By: Uma shankar at 2014-07-09 00:58:05

Your name (required):


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


Your sites URL (optional):


Your comments:



More Tutorials by Abinaya
How to compile a Java program - javac
The clone() Method in Java
Data Types in Java
JSP Example to connect to MS SQL database and retrieve records
faces-config.xml to DirectTraffic in the JSF Application
Enabling Expression Language Evaluation in JSP
Using malloc() Function in C
ActionErrors and ActionError in Struts
Open, Creat, Close, Unlink system calls sample program in C
Structures and Functions in C
Introduction to JSP expression language
Multi-dimensional Arrays in C (Explained using date conversion program)
A C program similar to grep command in UNIX
Type Conversions in C (String to Integer, isdigit() etc)
Basics of 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
What is Struts? Which Version of Struts to use?
Handling Duplicate Form Submissions in Struts
ActionErrors and ActionError in Struts
ForwardAction in Struts
7 Best Practices of Struts
Simple example of using the requiredif Validator rule in Struts
Handling multiple buttons in HTML Form in Struts
Configuring JDBC DataSources in Struts
MVC Architecture (Model 2 Architecture)
Installing and configuring Tomcat and Struts
DispatchAction in Struts
Using JavaScript to submit a form in Struts
Introduction to Struts Architecture
Most Emailed Articles (in Struts)
What is a Model View Controller (MVC) Model?
What is Struts? Which Version of Struts to use?
Is Struts the most popular web application framework for Java?
History of Struts
WebWork 2 is now Apache Struts 2
Struts Classes
MVC Architecture (Model 2 Architecture)
ActionErrors and ActionError in Struts
ForwardAction in Struts
Struts 1 vs Struts 2
Creating Struts Modules
Writing the first Struts application
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
Handling Duplicate Form Submissions in Struts