一,首先webwork的validator是基于拦截器的,所以首先要配制一下拦截器,默认的拦截器,已经
使用了validator,如果你想定义自己的拦截器组合,记得在你的拦截器的stack中把这句话加入
< param name = " excludeMethods " >
input,back,cancel,browse
</ param >
</ interceptor - ref >
但是如果你想自己定义,那么记得注册你的验证器(Validator),一个简单的注册办法是
添加一个文件名为 validators.xml 的文件在你的classpath (/WEB-INF/classes) 的根目录下
文件中的内容类似如下:
< validator name = " required " class = " com.opensymphony.xwork.validator.validators.RequiredFieldValidator " />
< validator name = " requiredstring " class = " com.opensymphony.xwork.validator.validators.RequiredStringValidator " />
< validator name = " int " class = " com.opensymphony.xwork.validator.validators.IntRangeFieldValidator " />
< validator name = " double " class = " com.opensymphony.xwork.validator.validators.DoubleRangeFieldValidator " />
< validator name = " date " class = " com.opensymphony.xwork.validator.validators.DateRangeFieldValidator " />
< validator name = " expression " class = " com.opensymphony.xwork.validator.validators.ExpressionValidator " />
< validator name = " fieldexpression " class = " com.opensymphony.xwork.validator.validators.FieldExpressionValidator " />
< validator name = " email " class = " com.opensymphony.xwork.validator.validators.EmailValidator " />
< validator name = " url " class = " com.opensymphony.xwork.validator.validators.URLValidator " />
< validator name = " visitor " class = " com.opensymphony.xwork.validator.validators.VisitorFieldValidator " />
< validator name = " conversion " class = " com.opensymphony.xwork.validator.validators.ConversionErrorFieldValidator " />
< validator name = " stringlength " class = " com.opensymphony.xwork.validator.validators.StringLengthFieldValidator " />
< validator name = " regex " class = " com.opensymphony.xwork.validator.validators.RegexFieldValidator " />
</ validators >
注意:
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中的缺省构造函数
<! 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 " >
<!-- 是否使用trim -->
< param name = " trim " > true </ param >
<!-- 提示的错误信息 -->
< message > You must enter a name </ message >
</ field - validator >
</ field >
< field name = " currDate " >
< field - validator type = " date " >
<!-- 最小时间 -->
< param name = " min " > 01 / 01 / 2007 </ param >
<!-- 最大时间 -->
< param name = " max " > 02 / 28 / 2007 </ param >
<!-- 提示的错误信息 -->
< message >
The date must be between 01 - 01 - 2007 and 02 - 28 - 2007 .
</ message >
</ field - validator >
</ field >
< field name = " url " >
<!-- short - circuit是否短路检验,类似 && 运算的短路算法 -->
< field - validator type = " required " short - circuit = " true " >
<!-- 提示的错误信息 -->
< message > You must enter a value for url. </ message >
</ field - validator >
<!-- webwork提供的url验证 -->
< field - validator type = " url " short - circuit = " true " >
<!-- 提示的错误信息 -->
< message > Not a valid url. </ message >
</ field - validator >
</ field >
< field name = " mail " >
< field - validator type = " required " >
< message > You must enter a value for 邮件. </ message >
</ field - validator >
<!-- mail的验证,验证器由webwork提供 -->
< field - validator type = " email " >
< message > Not a valid 邮件. </ message >
</ field - validator >
</ field >
< field name = " age " >
< field - validator type = " int " >
<!-- 同样的最大最小值 -->
< param name = " min " > 1 </ param >
< param name = " max " > 200 </ param >
< message >
<!-- 注意这里,msn中可以用表达式取出值栈中的值例如$ {min} -->
Only people ages $ {min} to $ {max} may be true ,otherwise
you are a ghost
</ message >
</ field - validator >
</ field >
< field name = " desc " >
< field - validator type = " requiredstring " >
< param name = " trim " > true </ param >
< message > You must enter a desc </ message >
</ field - validator >
< field - validator type = " stringlength " >
< param name = " minLength " > 1 </ param >
< param name = " maxLength " > 10 </ param >
<!-- 从属性文件中取出my.key显示,如果取不到my.key就显示
You must enter a desc length = 10 (my.key) -->
< message key = " my.key " >
You must enter a desc length = 10 (my.key)
</ message >
</ field - validator >
<!-- 可以使用正则表达式 -->
< field - validator type = " regex " >
< param name = " expression " >
<! [CDATA[([a - z][ 0 - 9 ][A - Z][x])]] >
</ param >
<!-- message可以由固定的字符串,值栈中的值$ {desc} 以及通过调用方法取到的
属性文件中的值$ {getText('my.key')} -->
< message >
" regex error ${getText('my.key')} ${desc} "
</ message >
</ field - validator >
</ field >
<!-- 上边的验证叫字段验证,下边这些验证叫普通验证,普通验证是优于字段验证的。
在使用短路的时候要小心 -->
<!-- 上边那些都是基于字段的验证,这个是基于Action的验证
如果要在页面显示这些消息,需要加入 < ww:actionError />
-->
< validator type = " expression " >
<!-- 可以基于表达式的验证name.equals(desc) -->
< param name = " expression " > name.equals(desc) </ param >
< message > name not the same as desc </ message >
</ validator >
<!-- Plain Validator 2 -->
<!-- 可以基于表达式的验证mail.startsWith( ' mark ' ) -->
< validator type = " expression " short - circuit = " true " >
< param name = " expression " > mail.startsWith( ' mark ' ) </ param >
< message > Email does not start with mark </ message >
</ validator >
</ validators >
四、客户端验证
加入validate="true"
<ww:form name="test" action="javascriptValidation" validate="true">
...
</ww:form>
这样的话webwork会根据你的服务器端的验证,对应生成javaScript的验证,而且提示信息和服务器端
验证的方式一样,而不是alert的方式显示。(前题是你使用的都是标准的验证器)
五、
对应上边验证器的web页面
<% @ taglib prefix = " ww " uri = " /webwork " %>
<! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< html >
< head >
< title ></ title >
< ww:head />
</ head >
< body >
<!--
如果想统一的显示所有的Erro打开下边这句
< ww:fielderror />
如果想显示其中一个Error打开下边这句,默认显示在字段的正上方
< ww:fielderror >
< ww:param value = " %{'name'} " />
</ ww:fielderror >
< ww:fielderror >
< ww:param > desc </ ww:param >
</ ww:fielderror >
-->
< ww:form name = " test " namespace = " / " action = " DbAdmin " method = " post " validate = " true " >
< ww:actionmessage />
< ww:actionerror />
< ww:textfield label = " name " name = " name " />
< ww:date id = " strCurrDate " name = " currDate " format = " yyyy-MM-dd " />
< ww:datepicker name = " currDate " id = " currDate " showstime = " true "
format = " %Y-%m-%e %H:%M " cssClass = " tx " language = " zh " required = " true "
template = " datepicker.ftl " label = " currDate " value = " %{strCurrDate} " >
</ ww:datepicker >
< ww:textfield label = " url " name = " url " />
< ww:textfield label = " mail " name = " mail " />
< ww:textfield label = " age " name = " age " />
< ww:textfield label = " desc " name = " desc " />
< ww:submit action = " DbAdmin " method = " doTestValidator "
value = " doTestValidator " />
</ ww:form >
</ body >
</ html >
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