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.