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

regajax.jsp

<%@taglib uri="/struts-tags"  prefix="s"%>
<%@taglib uri="/struts-dojo-tags"  prefix="sx"%>
<html>
    <head>
        <title>Ajax_Validation_Example</title>
    <sx:head/>
</head>
<body>
    <h2>Ajax Validation Example</h2>
<s:actionerror/>
    <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>
    </s:form>
</body>
</html>

success.jsp

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

2. Deployment Descriptor

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>regajax.jsp</welcome-file>
    </welcome-file-list>
</web-app>

3. Struts Configuration

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>    
    <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>
            <result>/success.jsp</result>
        </action>
    </package>
</struts>

4. Action Class

In com.javaknowledge.reg package
Register.java

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) {
        this.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;
    }

    @Override
    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
Register-validation.xml

<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
    <field name="name">
        <field-validator type="requiredstring">
            <message>Please enter your name</message>
        </field-validator>
    </field>
    <field name="pass">
        <field-validator type="requiredstring">
            <message>Password can't be blank</message>
        </field-validator>
  		
        <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-validator>
  		
    </field>
    <field name="userEmail">
        <field-validator type="email">        
            <message>Please enter  valid email id.</message>
        </field-validator>
    </field>   
    <field name="userEmail">
        <field-validator type="requiredstring">        
            <message>Please enter email</message>
        </field-validator>
    </field>        
</validators>

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
  6. javassist-3.7.ga.jar
  7. ognl-3.0.jar
  8. struts2-core-2.2.1.jar
  9. struts2-dojo-plugin-2.2.1.1.jar
  10. xwork-core-2.2.1.jar

struts2_ajax_validation

 

admin

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

4,880` Total Views

Leave a Reply

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


three × = 3