Apache Struts 2 Documentation > Home > Guides > Core Developers Guide > Validation > Client Side Validation > Pure JavaScript Client Side Validation > AJAX Client Side Validation > AJAX Validation |
|
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>
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>
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; } }
Create the validation.xml
<!-- Add the following DOCTYPE declaration as first line of your XXX-validation.xml file: <!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>You must enter a name</message> </field-validator> </field> <field name="age"> <field-validator type="int"> <param name="min">13</param> <param name="max">19</param> <message>Only people ages 13 to 19 may take this quiz</message> </field-validator> </field> </validators>