WebWork 2 : 校验
This page last changed on Dec 05, 2004 by plightbo.
WebWork依靠XWork的校验框架将应用的输入校验规则应用到活动中. 本节仅提供很少的信息让你可以上手并缺内容集中在This section only provides the bare minimum to get you started and focuses on WebWork为支持客户端校验而对XWork校验器进行的扩展. 更多信息请参阅XWork校验框架文档.参考内容 注册校验器校验规则包含在校验器中, 它必须在ValidatorFactory中注册. 简单的办法是在根类路径(/WEB-INF/classes)中添加文件validators.xml并在其中声明你使用的全部校验器. 文件的语法如下: <validators> <validator name="required" class="com.opensymphony.webwork.validators.JavaScriptRequiredFieldValidator"/> <validator name="requiredstring" class="com.opensymphony.webwork.validators.JavaScriptRequiredStringValidator"/> <validator name="stringlength" class="com.opensymphony.xwork.validator.validators.StringLengthFieldValidator"/> <validator name="int" class="com.opensymphony.webwork.validators.JavaScriptIntRangeFieldValidator"/> <validator name="date" class="com.opensymphony.webwork.validators.JavaScriptDateRangeFieldValidator"/> <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.webwork.validators.JavaScriptEmailValidator"/> <validator name="url" class="com.opensymphony.webwork.validators.JavaScriptURLValidator"/> <validator name="visitor" class="com.opensymphony.xwork.validators.VisitorFieldValidator"/> <validator name="conversion" class="com.opensymphony.xwork.validator.validators.ConversionErrorFieldValidator"/> </validators> 这里声明了WebWork附带的全部校验器. 打开校验激活对某活动校验的方法是将ValidationInterceptor加入活动的interceptor-refs中(参见xwork.xml): <interceptor name="validator" class="com.opensymphony.xwork.validator.ValidationInterceptor"/>
定义校验规则校验规则可以指定给:
下面是一个例子SimpleAction-validation.xml: <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd"> <validators> <field name="bar"> <field-validator type="required"> <message>You must enter a value for bar.</message> </field-validator> <field-validator type="int"> <param name="min">6</param> <param name="max">10</param> <message>bar must be between ${min} and ${max}, current value is ${bar}.</message> </field-validator> </field> <field name="date"> <field-validator type="date"> <param name="min">12/22/2002</param> <param name="max">12/25/2002</param> <message>The date must be between 12-22-2002 and 12-25-2002.</message> </field-validator> </field> <field name="foo"> <field-validator type="int"> <param name="min">0</param> <param name="max">100</param> <message key="foo.range">Could not find foo.range!</message> </field-validator> </field> <validator type="expression"> <param name="expression">foo > bar</param> <message>Foo must be greater than Bar. Foo = ${foo}, Bar = ${bar}.</message> </validator> </validators> 这里是SimpleAction类的校验文件. Validators(和field-validators)元素必须具备type属性, 它引用ValidatorFactory中注册的一个校验器的名字. Validator元素也可以包含<param>元素, 使用name和value属性将参数设置到校验器实例中. 下文是对message元素的讨论. 每个Validator或Field-Validator元素必需定义一个message元素. message元素有一个属性"key", 它不是必需的. 如果校验失败, message元素的正文(body)将作为默认消息加到活动中."key"属性定义了消息的键值, 如果活动实现了LocaleAware接口(ActionSupport类已经实现了这一接口), 将在活动的资源包中使用getText()方法查找相应的消息内容. 这提供了根据用户请求的区域设置(或任何设置到LocaleAware活动中的区域)选择对应的区域化消息的能力. 在使用key从资源包中获取消息或直接得到默认消息之后, 当前校验器被压入值栈, 然后将对消息进行解析, 将${...}部分用${和}之间的字符串的赋值结果进行替换. 可以使用校验器和活动对象来定义参数化消息. 下面是参数化消息的例子:bar must be between ${min} and ${max}, current value is ${bar}. 它将从IntRangeFieldValidator中取出min和max参数, 从活动中取出bar的值. |
Document generated by Confluence on Dec 14, 2004 16:36 |