Apache Struts 2 Documentation > Home > Guides > Core Developers Guide > Validation > Client Side Validation > Pure JavaScript Client Side Validation > AJAX Client Side Validation > AJAX Validation |

Apache Struts 2 Documentation
AJAX Validation
Edit Page Browse Space Add Page Add News
Added by tm_jee, last edited by Ted Husted on Jan 08, 2007 (view change)

## Description

| Ajax Validation requires DWR servlet being setup, Dojo and the Ajax theme being used. In the Ajax theme, DWR is used for normal validation while Dojo handles everything else (widgets, XHR, browser JS events etc.) In order for validation to function properly it is advised to used standard Struts Tags.

## Setup DWR

DWR could be setup by having the following dwr configuration (dwr.xml) at /WEB-INF/ directory. If it needs to be in other places, refer to http://getahead.ltd.uk/dwr/ for more information.

    <!DOCTYPE dwr PUBLIC
    	"-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN"
    	"http://www.getahead.ltd.uk/dwr/dwr10.dtd">

    <dwr>
        <allow>
            <create creator="new" javascript="validator">
                <param name="class" value="org.apache.struts2.validators.DWRValidator"/>
            </create>
            <convert converter="bean" match="com.opensymphony.xwork2.ValidationAwareSupport"/>
        </allow>

        <signatures>
            <![CDATA[
            import java.util.Map;
            import org.apache.struts2.validators.DWRValidator;

            DWRValidator.doPost(String, String, Map<String, String>);
            ]]>
        </signatures>
    </dwr>

A DWRServlet need to be registered with the web application as well. The following shows a typical web.xml with DWRSerlvet.

    <servlet>
           <servlet-name>dwr</servlet-name>
           <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
           <init-param>
               <param-name>debug</param-name>
               <param-value>true</param-value>
           </init-param>
       </servlet>

    <!-- JavaServer Faces Servlet Configuration, not used directly -->
     	<servlet>
       	<servlet-name>faces</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
       	<load-on-startup>1</load-on-startup>
     	</servlet>

     	<!-- JavaServer Faces Servlet Mapping, not called directly -->
       	<servlet-mapping>
           <servlet-name>faces</servlet-name>
           <url-pattern>*.action</url-pattern>
     	</servlet-mapping>

       <servlet-mapping>
           <servlet-name>dwr</servlet-name>
           <url-pattern>/dwr/*</url-pattern>
       </servlet-mapping>

### Step 1

Create the jsp page. Note the <ww:head ...> tag is used to set the theme which will put in necesary dojo sripts etc. See ajax's theme head.ftl for more information.

    <html>
    <head>
        <title>Validation - Basic</title>
        <s:head theme="ajax"/>
    </head>

    <body>

    <s:form method="post" validate="true" theme="ajax">
        <s:textfield label="Name" name="name"/>
        <s:textfield label="Age" name="age"/>
        <s:textfield label="Favorite color" name="answer"/>
        <s:submit/>
    </s:form>

    </body>
    </html>

### Step 2

Create the action class

    public class QuizAction extends ActionSupport {

        private static final long serialVersionUID = -7505437345373234225L;

        String name;
        int age;
        String answer;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getAnswer() {
            return answer;
        }

        public void setAnswer(String answer) {
            this.answer = answer;
        }
    }

### Step 3

Create the validation.xml

An error occurred: http://svn.apache.org/repos/asf/struts/struts2/trunk/apps/showcase/src/main/java/org/apache/struts2/showcase/validation/QuizAction-validation.xml. The system administrator has been notified.