Struts2 Ajax Validation

AJAX validation in Struts 2 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.

To use the AJAX validation, you need to add dojo plugin in your project.
Let’s create an example registration form.

1. JSP View Pages


<%@taglib uri="/struts-tags"  prefix="s"%>
<%@taglib uri="/struts-dojo-tags"  prefix="sx"%>
    <h2>Ajax Validation Example</h2>
    <s:form action="regValidation" >
        <s:textfield label="Name" name="name"></s:textfield>
        <s:password name="pass" label="Password"></s:password>
        <s:textfield name="userEmail" label="Email"></s:textfield>
        <sx:submit validate="true"> </sx:submit>


<%@taglib uri="/struts-tags" prefix="s" %>
        <title>Login Success</title>
        <h2>Ajax Validation Example</h2>
        <b> Welcome :  </b> <s:property value="name"/> <br/>

2. Deployment Descriptor


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="" xmlns:xsi="" xsi:schemaLocation="">


3. Struts Configuration


<?xml version="1.0" encoding="UTF-8" ?>
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    <package name="regValidation" extends="struts-default">
        <action name="regValidation" class="com.javaknowledge.reg.Register">
            <interceptor-ref name="jsonValidationWorkflowStack"/>
            <result name="input">/regajax.jsp</result>
            <result name="error">/regajax.jsp</result>

4. Action Class

In com.javaknowledge.reg package

package com.javaknowledge.reg;

import com.opensymphony.xwork2.ActionSupport;

public class Register extends ActionSupport {

    private String name;
    private String pass;
    private String userEmail;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getPass() {
        return pass;

    public void setPass(String pass) {
        this.pass = pass;

    public String getUserEmail() {
        return userEmail;

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;

    public String execute() throws Exception {
        System.out.println("Validating login");
        if ((!getName().equals("bari"))) {
            addActionError("Invalid user name! Please try again!");
            return ERROR;
        } else {
            return SUCCESS;

5. Bundle Validators

In com.javaknowledge.reg package

<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "">
    <field name="name">
        <field-validator type="requiredstring">
            <message>Please enter your name</message>
    <field name="pass">
        <field-validator type="requiredstring">
            <message>Password can't be blank</message>
        <field-validator type="stringlength">
            <param name="minLength">5</param>
            <param name="maxLength">10</param>
            <message>Password can't be less than 5 or greater than 10</message>
    <field name="userEmail">
        <field-validator type="email">        
            <message>Please enter  valid email id.</message>
    <field name="userEmail">
        <field-validator type="requiredstring">        
            <message>Please enter email</message>

Required Jars

  1. commons-fileupload-1.2.1.jar
  2. commons-io-1.3.2.jar
  3. commons-logging-1.0.4.jar
  4. commons-logging-api-1.1.jar
  5. freemarker-2.3.16.jar
  7. ognl-3.0.jar
  8. struts2-core-2.2.1.jar
  9. struts2-dojo-plugin-
  10. xwork-core-2.2.1.jar




Hello! I am Md. Abdul Bari; founder and admin of javaknowledge; currently providing training as a J2EE faculty in The Computers Ltd. Dhaka, Bangladesh under an IT Scholarship project provided by IDB-BISEW. I am a self learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.

More Posts - Website

Follow Me:
TwitterFacebookGoogle Plus

5,434` Total Views

Leave a Reply

Your email address will not be published. Required fields are marked *

7 × nine =