|
This validation mode only works with the ajax theme |
AJAX-based client side validation improves upon Pure JavaScript Client Side Validation by using a combination of JavaScript, DOM manipulation, and remote server communication via DWR.
Unlike the pure client side implementation, AJAX-based validation
communicates with the server. This means all your validation rules that
worked when submitting a form will still work within the browser.
AJAX基础上的客户端验证改进了Pure Javascript 客户端验证。它依赖于整合了Javascript,DOM操作,以及通过DWR和原断的server交互。不象纯javascript,AJAX-base的验证与服务器端交互。这意味着提交form后所有验证规则仍然在浏览器里工作。
The validation occurs on each onblur event for each form
element. As each user types in some values and moves to the next form
element, the value (and all other values previously entered) will be
sent to the server for validation. The entire validation stack is run,
including visitor validators and your action's validate() method.
Validation在每个元素的onblur事件后发生。当用户输入一些值,然后跳到下一个元素是,先输入的值将被送到服务端验证。整个验证栈正在运行,包括visitor validators和你的action的validate()方法。
If there is an error, like the pure implementation, the HTML and DOM will be updated immediately.
For an example of this, see AJAX Validation.
Description
|
- Ajax Validation requires DWR servlet being setup, Dojo and the Ajax theme being used.
Ajax Validation需要建立DWR Servlet,使用Dojo和Ajax主题。
- In the Ajax theme, DWR is used for normal validation while Dojo handles everything else (widgets, XHR, browser JS events etc.)
在Ajax 主题里,DWR是用作常规的验证,而Dojo处理其他事情(...)
- In order for validation to function properly it is advised to used standard Struts Tags.
为了使验证工作正常进行,建议使用标准的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.
DWR通过防止drw.xml到/WEB-INF/ 目录来建立。
<!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.
DWRServlet需要注册给web应用,下面展示了如何在web.xml中注册DWRServlet
<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>
<servlet>
<servlet-name>faces</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<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.
创建jsp页面。注意<s:head...>标签用来获取放置需要dojoscript等的主题。
<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.