it167.com  设为主页
 收藏本站
 
  资讯:业界动态 | 软件动态 | 人物专栏 | 安全资讯 | 网络生活 | 电子商务 | 小游戏 | 视频 | 美女图片 | 音乐
  网络编程 | 网站运营 | 网页制作 | 图形图象 | 操作系统 | 媒体动画 | 软件教学 | 网络应用 | 邮件系统 | 网络安全 | 认证考试
asp | .net | php | jsp | Sql | java | Dreamweaver | FrontPages | Javascript | css | Coreldraw | photoshop | Flash | Coreldraw
当前位置: > 主页>网络编程>Java>JAVA开发技巧>使用 Struts Validator(1)
最新新闻

·机会与整合 边缘化互联
·TOM-Skype新增三大本地
·雅虎抢闸邮箱竞赛 网易
·新浪抢攻北京奥运
·洞悉网络口碑的掘金机会
·拆解网络病毒黑金交易
·木马下载器近期出现新变
·《互联网周刊》第17期文
·Web2.0是否催生自吹自擂
·三张宝宝裸照招来MSN封
热门新闻
·Java SE 6 的HTTP 协议
·Java中对HashMap的深度
·Java调用Oracle的过程和
·开源技术 Eclipse使用技
·深入了解WebLogic的类装
·Java进阶:Struts多模块
·Java初学者入门经典:面
·Jave学习精华:Jsp小结
·Java学习:EJB的专用术语
·编程必备经典:Java常见
推荐新闻
 
 

使用 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 冗长的设置。



共13页: 上一页 [1] [2] [3] [4] 5 [6] [7] [8] [9] [10] [11] [12] [13] 下一页
文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【论坛讨论

   相关文章:
·用J2SE 1.4进行Internet安全编程(上)(1) ·J2EE项目开发经验二则
·J2EE WEB应用架构分析(1) ·J2EE中XML配置文件的读取处理
·Eclipse开发工具简介 ·论J2EE开发Web应用程序中的安全认证机制(

   文章评论:(0条)
  
 请留名: 匿名评论   点击查看所有评论 网管论坛
 

  责任编辑:it167  声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。