Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

Ajax Validation

Struts 2 provides support to ajax validation. In such case, page will not be refreshed or reloaded so it will make the performance fast. It is implicitly done using javascript i.e. used for the client side validation.

To use the AJAX validation, you need to add dojo plugin in your project.

jsonValidation Interceptor

The AJAX validation is performed by jsonValidation interceptor. It is not found in the default stack so we need to define it explicitly. It doesn’t perform any validation itself that is why it must be used with validation interceptor. It is found in the jsonValidationWorkflowStack, that includes jsonValidation, validation and workflow interceptors and basicstack.

Steps to perform AJAX Validation

The simple steps to perform AJAX Validation are as follows:

  1. create the form to get input from the user
  2. Inherit the ActionSupport class in your action
  3. Define the validation in validation.xml file
  4. Define result name input for the error message and register the jsonValidationWorkflowStack in struts.xml file

Example to perform ajax validation

In this example, we are creating 4 pages :

  1. index.jsp for input from the user.
  2. for business logic.
  3. Register-validation.xml for using bundled validators.
  4. struts.xml for defining the interceptors and results for the action.
  5. welcome.jsp for the view component.

1) Create index.jsp for input

This jsp page creates a form using struts UI tags. It receives name, password and email id from the user.


  1. <%@ taglib  uri=“/struts-tags” prefix=“s”%>
  2. <%@ taglib uri=“/struts-dojo-tags” prefix=“d”%>
  3. <html>
  4. <head>
  5. <d:head/>
  6. </head>
  7. <body>
  8. <marquee>Registration Form………….</marquee>
  9. <s:form action=“register”>
  10. <s:textfield name=“name” label=“Username”></s:textfield>
  11. <s:textfield name=“email” label=“Email ID”></s:textfield>
  12. <s:password name=“password” label=“Password”></s:password>
  13. <d:submit validate=“true” type=“image” src=“register-now.jpg”>
  14. </d:submit>
  15. </s:form>
  16. </body>
  17. </html>

2) Create the action class

This action class inherits the ActionSupport class and overrides the execute method.

  1. package mypack;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. public class Register extends ActionSupport{
  4. private String name,password,email;
  5. //setters and getters
  6. public String execute(){
  7.     return “success”;
  8. }
  9. }

3) Create the validation file

Here, we are using bundled validators to perform the validation.


  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <!DOCTYPE validators PUBLIC
  3.   “-//OpenSymphony Group//XWork Validator 1.0.2//EN”
  4.   “”>
  5.     <validators>
  6.     <field name=“name”>
  7.     <field-validator type=“requiredstring”>
  8.     <message>Name can’t be blank</message>
  9.     </field-validator>
  10.     </field>
  11.     <field name=“email”>
  12.     <field-validator type=“requiredstring”>
  13.     <message>Email ID can’t be blank</message>
  14.     </field-validator>
  15.     <field-validator type=“email”>
  16.     <message>Please enter a valid email ID</message>
  17.     </field-validator>
  18.     </field>
  19.     <field name=“password”>
  20.     <field-validator type=“requiredstring”>
  21.     <message>Password can’t be blank</message>
  22.     </field-validator>
  23.     <field-validator type=“stringlength”>
  24.     <param name=“minLength”>5</param>
  25.     <param name=“maxLength”>10</param>
  26.     <message>Password can’t be less than 5 or greater than 10</message>
  27.     </field-validator>
  28.     </field>
  29.     </validators>

4) Create struts.xml

This xml file defines an extra result by the name input, and an interceptor jsonValidatorWorkflowStack.


  1. <?xml version=“1.0” encoding=“UTF-8” ?>
  2. <!DOCTYPE struts PUBLIC “-//Apache Software Foundation//DTD Struts
  3.  Configuration 2.1//EN” “”>
  4. <struts>
  5. <package name=“a” extends=“struts-default”>
  6. <action name=“register” class=“mypack.Register”>
  7. <interceptor-ref name=“jsonValidationWorkflowStack”></interceptor-ref>
  8. <result name=“success”>/welcome.jsp</result>
  9. <result name=“input”>/index.jsp</result>
  10. </action>
  11. </package>
  12. </struts>

5) Create view component

It is the simple jsp file displaying the information of the user.


  1. <%@ taglib uri=“/struts-tags” prefix=“s” %>
  2. Welcome, <s:property value=“name”/>