使用 Struts Validator(1)
作者: 来源:it167 点击: 日期:2007-01-28 |
|
|
不要太过担心这个文件的复杂性;除非要编写自己的验证规则,否则只要接受并使用它的默认版本就可以了。
向应用程序添加错误消息
大多数版本的 validation-rules.xml 在文件头的注释中,都包含像清单 6 这样的内容:
清单 6. validation-rules.xml 注释
These are the default error messages associated with
each validator defined in this file. They should be
added to your projects ApplicationResources.properties
file or you can associate new ones by modifying the
pluggable validators msg attributes in this file. # Struts Validator Error Messages
errors.required={0} is required.
errors.minlength={0} can not be less than {1} characters.
errors.maxlength={0} can not be greater than {1} characters.
errors.invalid={0} is invalid. errors.byte={0} must be a byte.
errors.short={0} must be a short.
errors.integer={0} must be an integer.
errors.long={0} must be a long.
errors.float={0} must be a float.
errors.double={0} must be a double. errors.date={0} is not a date.
errors.range={0} is not in the range {1} through {2}.
errors.creditcard={0} is an invalid credit card number.
errors.email={0} is an invalid e-mail address.
|
注释顶部的消息是条好建议。不管您是否遵照指示操作,Validator 在输出错误消息时都会采用这些属性。如果没有把这些插入应用程序的资源,结果就是空的错误消息,当然看起来就像根本没有错误消息一样。对于用户来说,再也没有什么会比提交表单之后、表单被拒绝、然后出错的时候没有任何反馈这件事更郁闷的了。
这些属性最好放在文件中,例如 MessageResources.properties,通常位于应用程序的 WEB-INF/classes 目录中:
清单 7. MessageResources.properties
<b># -- standard errors --
errors.header=<UL>
errors.prefix=<LI>
errors.suffix=</LI>
errors.footer=</UL>
# -- validator --
errors.invalid={0} is invalid.
errors.maxlength={0} can not be greater than {1} characters.
errors.minlength={0} can not be less than {1} characters.
errors.range={0} is not in the range {1} through {2}.
errors.required={0} is required.
errors.byte={0} must be an byte.
errors.date={0} is not a date.
errors.double={0} must be an double.
errors.float={0} must be an float.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.short={0} must be an short.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.
# -- other --
errors.cancel=Operation cancelled.
errors.detail={0}
errors.general=The process did not complete. Details should follow.
errors.token=Request could not be completed. Operation is not in sequence.</b>
# -- welcome --
welcome.title=Struts Validator Test Application
welcome.heading=Welcome to the Validation Tester Application!
welcome.message=This is a simple application meant to test and demonstrate the Struts Validator component.
welcome.test-validation=Test out some simple uses of the Struts Validator
|
但是,还是不用着急做这些修改,因为有更容易的方式(研究过 WEB-INF/classes/MessageResources.properties 文件的人可能对这个快捷方式已经有了认识)。如果想使用不同的错误消息,可以在这里做修改。例如,如果想让无效电子邮件地址的出错更好一些,可以把它改成像下面这样:
errors.email=You have entered an invalid e-mail address ({0}). Please try again.
|
这是非常好的定制错误消息的方法,不用触及实际的代码行(从而避免了重新编译、重新部署和许多重新测试)。
把 Validator 连接到应用程序
现在需要让 Struts 应用程序知道 Validator。在这里,我指的是组件的整体,而不是 Validator 的某个特定应用(这是我将在 在应用程序中使用 Validator 中介绍的内容)。将需要使用 Struts 的 plugin 元素,这是让 Struts 知道它应当集成进应用程序的组件的方法。在 struts-config.xml 文件中需要以下条目,这个文件位于应用程序的 WEB-INF 目录下:
<!-- =================================================== Validator plugin --> <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
|
className 属性告诉 Struts 要装载哪个类;这个类应当实现了 Struts 的 PlugIn 接口,就像 ValidatorPlugIn 做的那样。然后,可能有许多特定于插件的 set-property 条目;对于 Validator,只需要一个:把 pathnames 属性设置为 validator-rules.xml 和 validation.xml 文件所在路径的值。如果想把这些值保存在其他地方,可以在 plugin 元素中指定替代位置。
这些步骤完成之后,需要做的就是编写特定于应用程序的表单、动作和验证需求的 validation.xml 文件。但是,先让我提供一些快捷方式,以避免 Validator 冗长的设置。
|