WebWork的强大的验证器
来源:百度文库 编辑:神马文学网 时间:2024/10/04 23:33:40
webwork提供了强大的验证功能,下边一一介绍一些常用的功能的用法。
一,首先webwork的validator是基于拦截器的,所以首先要配制一下拦截器,默认的拦截器,已经
使用了validator,如果你想定义自己的拦截器组合,记得在你的拦截器的stack中把这句话加入
input,back,cancel,browse
二、是否使用自定义Validator:在一般情况下webwork提供的Validator已经能应付大多数问题,
但是如果你想自己定义,那么记得注册你的验证器(Validator),一个简单的注册办法是
添加一个文件名为 validators.xml 的文件在你的classpath (/WEB-INF/classes) 的根目录下
文件中的内容类似如下:
注意:
1,validators.xml如果已经定义了,那么它应该在classpath中可以找到.然而如果不需要自定义的校验器,那么这不是必须的.WebWork会自动从发布包里的
xwork jar文件中取得一个事先定义好的校验器集合(com/opensymphony/xwork/validator/validators/default.xml).
浏览ValidatorFactory的static块来了解详细信息
2,如果自定义的校验器被定义了而且创建了一个validators.xml文件并放在classpath中,
记得复制所有其他你需要的预定义的校验器到validators.xml里,如果你不需要注册则不需要.
一旦validators.xml在classpath里被检测到,缺省的 (com/opensymphony/xwork/validator/validators/default.xml)
就不会被装载了.只有没发现自定义 validators.xml的时候才会装载.要小心.这点类似Java中的缺省构造函数
三、定义校验规则
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
true
You must enter a name
01/01/2007
02/28/2007
The date must be between 01-01-2007 and 02-28-2007.
You must enter a value for url.
Not a valid url.
You must enter a value for 邮件.
Not a valid 邮件.
1
200
Only people ages ${min} to ${max} may be true,otherwise
you are a ghost
true
You must enter a desc
1
10
You must enter a desc length=10(my.key)
"regex error ${getText(‘my.key‘)} ${desc}"
name.equals(desc)
name not the same as desc
mail.startsWith(‘mark‘)
Email does not start with mark
四、客户端验证
加入validate="true"
...
这样的话webwork会根据你的服务器端的验证,对应生成javaScript的验证,而且提示信息和服务器端
验证的方式一样,而不是alert的方式显示。(前题是你使用的都是标准的验证器)
五、
对应上边验证器的web页面
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="ww" uri="/webwork"%>
format="%Y-%m-%e %H:%M" cssClass="tx" language="zh" required="true"
template="datepicker.ftl" label="currDate" value="%{strCurrDate}" >
value="doTestValidator" />
六、对应上边的Action代码
package niis.web.actions.temp;
import java.text.SimpleDateFormat;
import java.util.Date;
import niis.persistence.ITestDao;
import niis.persistence.dao.SqlCommand;
import niis.web.actions.AbstractAction;
public class DbAdminAction {
private String name;
private Date currDate ;
private String url;
private String mail;
private int age;
private String desc;
public String doTestValidator(){
System.out.println(name);
System.out.println(currDate);
System.out.println(url);
System.out.println(mail);
System.out.println(age);
return SUCCESS;
}
/** *//**
* @return the age
*/
public int getAge() {
return age;
}
/** *//**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/** *//**
* @return the date
*/
public Date getCurrDate() {
return currDate;
}
/** *//**
* @param date the date to set
*/
public void setCurrDate(Date date) {
this.currDate = date;
}
/** *//**
* @return the mail
*/
public String getMail() {
return mail;
}
/** *//**
* @param mail the mail to set
*/
public void setMail(String mail) {
this.mail = mail;
}
/** *//**
* @return the name
*/
public String getName() {
return name;
}
/** *//**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/** *//**
* @return the url
*/
public String getUrl() {
return url;
}
/** *//**
* @param url the url to set
*/
public void setUrl(String url) {
this.url = url;
}
/** *//**
* @return the testDao
*/
public ITestDao getTestDao() {
return testDao;
}
/** *//**
* @return the desc
*/
public String getDesc() {
return desc;
}
/** *//**
* @param desc the desc to set
*/
public void setDesc(String desc) {
this.desc = desc;
}
}
七、简单提示:属性文件的名字类名+properties,validator配制文件的名字类名+validation.xml
(还有其它的组合方式)
例如对应的DbAdminAction.java
DbAdminAction-validation.xml
DbAdminAction.properties
注意
许多WebWork标签的 required 属性和客户端校验没有什么关系. 它只是在某个theme(例如xhtml)中用来在一个标识为必填的字段周围放置一个‘*‘.
八、上边程序的示例代码(从eclipse导出的工程)
http://www.blogjava.net/Files/dreamstone/vl.rar
一,首先webwork的validator是基于拦截器的,所以首先要配制一下拦截器,默认的拦截器,已经
使用了validator,如果你想定义自己的拦截器组合,记得在你的拦截器的stack中把这句话加入
input,back,cancel,browse
二、是否使用自定义Validator:在一般情况下webwork提供的Validator已经能应付大多数问题,
但是如果你想自己定义,那么记得注册你的验证器(Validator),一个简单的注册办法是
添加一个文件名为 validators.xml 的文件在你的classpath (/WEB-INF/classes) 的根目录下
文件中的内容类似如下:
注意:
1,validators.xml如果已经定义了,那么它应该在classpath中可以找到.然而如果不需要自定义的校验器,那么这不是必须的.WebWork会自动从发布包里的
xwork jar文件中取得一个事先定义好的校验器集合(com/opensymphony/xwork/validator/validators/default.xml).
浏览ValidatorFactory的static块来了解详细信息
2,如果自定义的校验器被定义了而且创建了一个validators.xml文件并放在classpath中,
记得复制所有其他你需要的预定义的校验器到validators.xml里,如果你不需要注册则不需要.
一旦validators.xml在classpath里被检测到,缺省的 (com/opensymphony/xwork/validator/validators/default.xml)
就不会被装载了.只有没发现自定义 validators.xml的时候才会装载.要小心.这点类似Java中的缺省构造函数
三、定义校验规则
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
true
01/01/2007
02/28/2007
The date must be between 01-01-2007 and 02-28-2007.
1
200
Only people ages ${min} to ${max} may be true,otherwise
you are a ghost
true
1
10
You must enter a desc length=10(my.key)
"regex error ${getText(‘my.key‘)} ${desc}"
name.equals(desc)
mail.startsWith(‘mark‘)
四、客户端验证
加入validate="true"
...
这样的话webwork会根据你的服务器端的验证,对应生成javaScript的验证,而且提示信息和服务器端
验证的方式一样,而不是alert的方式显示。(前题是你使用的都是标准的验证器)
五、
对应上边验证器的web页面
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="ww" uri="/webwork"%>
template="datepicker.ftl" label="currDate" value="%{strCurrDate}" >
六、对应上边的Action代码
package niis.web.actions.temp;
import java.text.SimpleDateFormat;
import java.util.Date;
import niis.persistence.ITestDao;
import niis.persistence.dao.SqlCommand;
import niis.web.actions.AbstractAction;
public class DbAdminAction {
private String name;
private Date currDate ;
private String url;
private String mail;
private int age;
private String desc;
public String doTestValidator(){
System.out.println(name);
System.out.println(currDate);
System.out.println(url);
System.out.println(mail);
System.out.println(age);
return SUCCESS;
}
/** *//**
* @return the age
*/
public int getAge() {
return age;
}
/** *//**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/** *//**
* @return the date
*/
public Date getCurrDate() {
return currDate;
}
/** *//**
* @param date the date to set
*/
public void setCurrDate(Date date) {
this.currDate = date;
}
/** *//**
* @return the mail
*/
public String getMail() {
return mail;
}
/** *//**
* @param mail the mail to set
*/
public void setMail(String mail) {
this.mail = mail;
}
/** *//**
* @return the name
*/
public String getName() {
return name;
}
/** *//**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/** *//**
* @return the url
*/
public String getUrl() {
return url;
}
/** *//**
* @param url the url to set
*/
public void setUrl(String url) {
this.url = url;
}
/** *//**
* @return the testDao
*/
public ITestDao getTestDao() {
return testDao;
}
/** *//**
* @return the desc
*/
public String getDesc() {
return desc;
}
/** *//**
* @param desc the desc to set
*/
public void setDesc(String desc) {
this.desc = desc;
}
}
七、简单提示:属性文件的名字类名+properties,validator配制文件的名字类名+validation.xml
(还有其它的组合方式)
例如对应的DbAdminAction.java
DbAdminAction-validation.xml
DbAdminAction.properties
注意
许多WebWork标签的 required 属性和客户端校验没有什么关系. 它只是在某个theme(例如xhtml)中用来在一个标识为必填的字段周围放置一个‘*‘.
八、上边程序的示例代码(从eclipse导出的工程)
http://www.blogjava.net/Files/dreamstone/vl.rar
WebWork的强大的验证器
WebWork In Action学习笔记(一) WebWork的概貌 - 吾将上下而求索...
webwork doc:webwork Action中获取request, response对象的方法
WebWork/struts2中格式化输出数字和日期的方法
Struts的验证--Validator
验证码的故事
!验证码的最高境界
有关验证码的说明
有关验证码的说明
验证与确认的区别
简单验证身份证的合法性
经过验证的十二个鼻炎
制药冻干机的GMP验证
[转载]一阳五线的总结验证
一款非常强大的多功能播放器
心灵的强大
中国强大的军事实力
以色列强大的秘密
中国的强大
以色列强大的秘密
中国经济强大的背后.........
强大的实用软件
强大的解放军
强大的中国空军实力