Welcome!

Java Authors: Maureen O'Gara, Bruce Armstrong, Liz McMillan, Walter H. Pinson, III, Yakov Werde

Related Topics: AJAX & REA, Java

AJAX & REA: Article

Struts Validations Framework Using AJAX

Real-time data validation is one of the advantages of AJAX technology

Build Action and Action Form
For the Struts action, we just forward to the JSP we already built. The Action code will look as follows:

public ActionForward execute(...) {
return mapping.findForward("success");
}

The Action Form code will validate the requiredText property if the input is blank. Remember to extend this form from the AjaxForm class. The validate method of action form will appear as follows:

public ActionErrors validate(...) {
   ActionErrors errors = new ActionErrors();
   if (StringUtils.isEmpty(this.requiredText)) {
   errors.add("requiredText", new ActionError("error.required.input"));
   }
   request.setAttribute(Globals.ERROR_KEY, errors);
}

Applying Struts Validation Rules
The configure Struts validation rule, such as minimum length, maximum length, e-mail, and pattern text, will be applied to the input object of the client and the configuration will be similar to Listing 4.

Validation Processing Flow
First the client will initiate the XMLHTTP component to perform the request to the server and, then, on the event the user trigger starts to build, the parameter of the URL will be sent to the server. After its finish building the parameter, the client will attach the event of the oneadystatechange XMLHTTP to listen to the response from the server-side. When the response is accepted, the client side will start to parse the XML validation message and set the message into the right area (see Figure 6).

When the request is accepted to the server, the server will start to check the parameter of the AJAX validation condition and process the validation. Once it's finished, the errors object generated will be filtered into the specific error that is related to the user input object. From this point, the XML message will be generated after the filtering process is done and sent back to the client (see Figure 7).

Summary
In this article we built a controller that has the ability to receive asynchronous requests from the client and incorporate with the struts validation process to produce the action error object. Filtering the specific input object being validated will be done after the error object produces and generates the XML message as a reply to the client-side to indicate the error message (See Image 8 and Image 9).

More Stories By Sonny Hastomo

Sonny Hastomo is an IT architect at Sun Microsystems, Indonesia, for the telecommunication industry division. His currently is focusing on providing solution design, sizing, implementation, consulting services, and quality support to customers in their evaluation of their IT challenges.

Comments (9) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
hanan mahmoud 07/27/08 03:39:45 AM EDT

hi all
its realy great article and it helped me to understand validation in ajax better ,i just hope if any one have sample code about that topic because till now i don't know how to find one and also i don't know where to put the code samples in the article so plz help me

Commenter 05/17/06 01:40:52 PM EDT

I just need examples, and I can´t find it here. This article is weak.

SYS-CON Belgium News Desk 05/02/06 03:27:56 PM EDT

Struts Validations Framework Using AJAX
Real-time data validation is one of the advantages of AJAX technology. By applying this technology, the struts validation framework will enrich the struts MVC and move the Web application closer to the desktop application.

SYS-CON Australia News Desk 05/02/06 01:44:38 PM EDT

Real-time data validation is one of the advantages of AJAX technology. By applying this technology, the struts validation framework will enrich the struts MVC and move the Web application closer to the desktop application.

AJAX News Desk 05/02/06 12:55:46 PM EDT

AjaxWorld: Struts Validations Framework Using AJAX
Real-time data validation is one of the advantages of AJAX technology. By applying this technology, the struts validation framework will enrich the struts MVC and move the Web application closer to the desktop application.

Rajesh 05/02/06 12:17:24 PM EDT

The article was really nice.It would have been better if the source code was included.

Thanks

waikit 02/08/06 04:11:11 AM EST

can I have a sample code of the example shown in this article, please

regards

SYS-CON Australia News Desk 01/27/06 02:21:45 PM EST

Real-time data validation is one of the advantages of AJAX technology. By applying this technology, the struts validation framework will enrich the struts MVC and move the Web application closer to the desktop application.

Artem Vasiliev 01/26/06 06:18:56 AM EST

Sonnie, thank you for your article, it brings pretty interesting idea.
It would be great to see some sample application (with source code of course) implementing this approach. I even thought that I would find as a source code for the article, but seems like JDJ doesn't have such practice. Sonnie, can you provide that? It could be an archive hosted somewhere, here or at some free hosting, e.g. http://rapidshare.de/