ActionErrors and ActionError in Struts
By: Abinaya
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.
- 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.
- 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.
Archived Comments
1. In the case of Basic validation is it must to specify
<action .......
validate=&qu
View Tutorial By: Uma shankar at 2014-07-09 00:58:05
2. Please provide me a particular flow of validations to be performed on a textbox property in struts 1
View Tutorial By: Deepali at 2013-05-10 10:38:18
3. thanks for posting
View Tutorial By: sriram at 2011-11-24 06:38:42
4. @veera reddy,
the use of creating actionerrors from super class validate() method has to have
View Tutorial By: San at 2011-08-28 23:14:39
5. why do't you create ActionErrors object like this
ActionErrors errors=new ActionErrors();?
View Tutorial By: veera reddy at 2011-07-15 12:14:57
6. the default implementation of validate returns null so
adding to errors after a call
View Tutorial By: Jerry at 2009-02-01 11:12:06
7. jhjhj
View Tutorial By: hjhjhjhjhjhjh at 2008-12-23 06:53:27
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Configuring JDBC DataSources in Struts
When is the best time to validate input in Struts
Simple example of using the requiredif Validator rule in Struts
How to prepopulate a form in Struts
Using JavaScript to submit a form in Struts
FAQ: Why are my checkboxes not being set from ON to OFF?
FAQ: Why was reload removed from Struts (since 1.1)?
What is a Plug-in and how to use Java plug-ins with Struts?
Origin and Architecture of Struts
Handling multiple buttons in HTML Form in Struts