AJAX validation using Struts2 Framework and XML file with Eclipse Indigo

In this tutorial we are performing Server Side Validation using AJAX validation which will lead to the page to show validation errors without reloading the page.
To perform AJAX Validation we need to use  Struts2-dojo-plugin jar in our project.

so firstly download all Required Jars Without dojo-plugin

Download only Struts2-dojo-plugin-2.3.8.jar if you have already downloaded other jars from previous tutorials
or you can also download source code which is available in the end of this tutorial that contains all jars(with dojo-plugin) including source code

Project Structure in Project Explorer:
reg_form.jsp
create a user input page. 
To work with AJAX, <sx:head> must be in the page. Don't use validate="true" in the form tag because it invokes client side validation(Javascript) before Server Side Validation(AJAX validation) but use validate="true" in the submit tag to perform AJAX validation.
 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
      pageEncoding="ISO-8859-1"%>  
      <%@ taglib prefix="s" uri="/struts-tags"%>  
      <%@ taglib prefix="sx" uri="/struts-dojo-tags" %>  
      <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
 <title>Ajax-Registration-form</title>  
 <sx:head />  
 </head>  
 <body bgcolor="skyblue">  
      <h1>Ajax Registration Form</h1>  
      <hr>  
                  <s:form action="register.action" method="post" >  
            <s:textfield name="Name" label="Name" size="20" />  
            <s:radio name="Gender" label="Gender" list="{'Male', 'Female'}" />  
            <s:select name="Course" label="Course" list="{'Select-Option','B.Tech', 'MCA', 'MSC'}" />  
            <sx:submit align="center" validate="true" />  
         </s:form>  
 </body>  
 </html>  

success.jsp
create a user's success page.
 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
   pageEncoding="ISO-8859-1"%>  
   <%@ taglib prefix="s" uri="/struts-tags"%>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
 <title>success</title>  
 </head>  
 <body bgcolor="lightblue">  
 <center><h3>  
 Hello! <s:property value="name"/></h3></center>  
 </body>  
 </html>  

ContactAction.java
create a Action java class.
 package in.blog.webideaworld;  
 import com.opensymphony.xwork2.ActionSupport;  
 @SuppressWarnings("serial")  
 public class ContactAction extends ActionSupport {  
      private String name;  
      private String gender;  
      private String course;  
      public String getName() {  
           return name;  
      }  
      public void setName(String name) {  
           this.name = name;  
      }  
      public String getGender() {  
           return gender;  
      }  
      public void setGender(String gender) {  
           this.gender = gender;  
      }  
      public String getCourse() {  
           return course;  
      }  
      public void setCourse(String course) {  
           this.course = course;  
      }  
      public String execute() throws Exception{  
           return SUCCESS;  
      }  
 }  

ContactAction-validation.xml
Create validators in XML file and the format for the validatiors xml file is <ActionClassName>-validation.xml
 <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"  
           "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">  
 <validators>  
      <field name="Name">  
           <field-validator type="requiredstring">  
                <message>Name is Required.</message>  
           </field-validator>  
           <field-validator type="regex">  
       <param name="expression">[a-zA-Z]{2,20}</param>  
       <message>Please enter valid name.</message>  
     </field-validator>  
      </field>  
      <field name="Gender">  
           <field-validator type="requiredstring">  
                <message>Gender is Required.</message>  
           </field-validator>  
      </field>  
      <field name="Course">  
           <field-validator type="regex">  
       <param name="expression">B.Tech|MCA|MSC</param>  
       <message>Course is required.</message>  
     </field-validator>  
      </field>  
 </validators>  

struts.xml
set result name="success" for success.jsp and name="input" for reg_form.jsp for validation messages to show up on input page.
 <?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="default" namespace="/" extends="struts-default">  
     <action name="register" class="in.blog.webideaworld.ContactAction" method="execute" >  
     <result name="success">success.jsp</result>  
     <result name="input">reg_form.jsp</result>  
     </action>  
   </package>  
   </struts>  

web.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  
  <display-name>Struts2_Ajax_Validate</display-name>  
  <welcome-file-list>  
     <welcome-file>reg_form.jsp</welcome-file>  
  </welcome-file-list>  
  <filter>  
    <filter-name>struts2</filter-name>  
    <filter-class>  
      org.apache.struts2.dispatcher.FilterDispatcher  
    </filter-class>  
   </filter>  
   <filter-mapping>  
    <filter-name>struts2</filter-name>  
    <url-pattern>/*</url-pattern>  
   </filter-mapping>  
 </web-app>  

Download Code

View in Browser: 

Registration form:

checking validation without input values:

success page:

More Struts Topics :

No comments: