|
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基礎(chǔ)上的客戶端驗(yàn)證改進(jìn)了Pure Javascript 客戶端驗(yàn)證。它依賴于整合了Javascript,DOM操作,以及通過(guò)DWR和原斷的server交互。不象純javascript,AJAX-base的驗(yàn)證與服務(wù)器端交互。這意味著提交form后所有驗(yàn)證規(guī)則仍然在瀏覽器里工作。
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在每個(gè)元素的onblur事件后發(fā)生。當(dāng)用戶輸入一些值,然后跳到下一個(gè)元素是,先輸入的值將被送到服務(wù)端驗(yàn)證。整個(gè)驗(yàn)證棧正在運(yùn)行,包括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是用作常規(guī)的驗(yàn)證,而Dojo處理其他事情(...)
- In order for validation to function properly it is advised to used standard Struts Tags.
為了使驗(yàn)證工作正常進(jìn)行,建議使用標(biāo)準(zhǔn)的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通過(guò)防止drw.xml到/WEB-INF/ 目錄來(lái)建立。
<!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需要注冊(cè)給web應(yīng)用,下面展示了如何在web.xml中注冊(cè)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.
創(chuàng)建jsp頁(yè)面。注意<s:head...>標(biāo)簽用來(lái)獲取放置需要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.