webwork提供了强大的验证功能,下边一一介绍一些常用的功能的用法。
一,首先webwork的validator是基于拦截器的,所以首先要配制一下拦截器,默认的拦截器,已经
使用了validator,如果你想定义自己的拦截器组合,记得在你的拦截器的stack中把这句话加入
<
interceptor
-
ref name
=
"
validation
"
>
<
param name
=
"
excludeMethods
"
>
input,back,cancel,browse
</
param
>
</
interceptor
-
ref
>
二、是否使用自定义Validator:在一般情况下webwork提供的Validator已经能应付大多数问题,
但是如果你想自己定义,那么记得注册你的验证器(Validator),一个简单的注册办法是
添加一个文件名为 validators.xml 的文件在你的classpath (/WEB-INF/classes) 的根目录下
文件中的内容类似如下:
<
validators
>
<
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
>
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<!--
注意这里,msn中可以用表达式取出值栈中的值例如$
{min}
-->
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
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
>
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<!--
message可以由固定的字符串,值栈中的值$
{desc}
以及通过调用方法取到的
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
属性文件中的值$
{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页面
<%
@page contentType
=
"
text/html; charset=UTF-8
"
pageEncoding
=
"
UTF-8
"
%>
<%
@ 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
"
一,首先webwork的validator是基于拦截器的,所以首先要配制一下拦截器,默认的拦截器,已经
使用了validator,如果你想定义自己的拦截器组合,记得在你的拦截器的stack中把这句话加入
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
但是如果你想自己定义,那么记得注册你的验证器(Validator),一个简单的注册办法是
添加一个文件名为 validators.xml 的文件在你的classpath (/WEB-INF/classes) 的根目录下
文件中的内容类似如下:
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
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中的缺省构造函数
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
![dot.gif](/Images/dot.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
加入validate="true"
<ww:form name="test" action="javascriptValidation" validate="true">
...
</ww:form>
这样的话webwork会根据你的服务器端的验证,对应生成javaScript的验证,而且提示信息和服务器端
验证的方式一样,而不是alert的方式显示。(前题是你使用的都是标准的验证器)
五、
对应上边验证器的web页面
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)