Product Reviews
Struts Validations Framework Using AJAX
Real-time data validation is one of the advantages of AJAX technology
Apr. 6, 2007 10:00 AM
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).
About Sonny HastomoSonny 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.
#9 |
hanan mahmoud commented on 27 Jul 2008
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
|
#8 |
Commenter commented on 17 May 2006
I just need examples, and I can´t find it here. This article is weak.
|
#7 |
SYS-CON Belgium News Desk commented on 2 May 2006
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.
|
#6 |
SYS-CON Australia News Desk commented on 2 May 2006
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.
|
#5 |
AJAX News Desk commented on 2 May 2006
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.
|
#4 |
Rajesh commented on 2 May 2006
The article was really nice.It would have been better if the source code was included.
Thanks
|
#3 |
waikit commented on 8 Feb 2006
can I have a sample code of the example shown in this article, please
regards
|
#2 |
SYS-CON Australia News Desk commented on 27 Jan 2006
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.
|
#1 |
Artem Vasiliev commented on 26 Jan 2006
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/
|