一:在Webwork中使用Ajax做验证必须注意以下三点:
1、设置Dwr Servlet,使用dojo和ajax的主题。
2:在ajax的主题里面,dwr作为普通的验证处理。dojo处理其它的验证,如Widgets,XHR,Browser js events等。
3:建议使用标准的Webwork tags。
二:设置Dwr:
1:建立dwr.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="com.opensymphony.webwork.validators.DWRValidator"/>
</create>
<convert converter="bean" match="com.opensymphony.xwork.ValidationAwareSupport"/>
</allow>
<signatures>
<![CDATA[
import java.util.Map;
import com.opensymphony.webwork.validators.DWRValidator;
DWRValidator.doPost(String, String, Map<String, String>);
]]>
</signatures></dwr>
2:在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-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
三:使用
1:建立jsp页,使用<ww:head...>设置主题为ajax,该设置能将必要的dojo的代码放在页面中。
<html>
<head>
<title>Validation - Basic</title>
<ww:head theme="ajax"/>
</head>
<body>
<!-- 注意设置validate和theme -->
<ww:form method="post" validate="true" theme="ajax">
<ww:textfield label="Name" name="name"/>
<ww:textfield label="Age" name="age"/>
<ww:textfield label="Favorite color" name="answer"/>
<ww:submit/></ww:form></body></html>
2:建立Action类:
public class QuizAction extends ActionSupport {
String name;
int age;
String answer;
publicString getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
publicint getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
publicString getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
}
3:建立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>
posted on 2006-10-21 23:43
不做浮躁的人 阅读(1573)
评论(0) 编辑 收藏