java几种对象验证框架

(1) Apache Commons Validator

Commons -Validator包用来把验证规则程序提取出来,以供重复使用。这个包可以使用在Struts中,也可以独立的应用在任何其它的应用中。用户可以通过 java类的方式自定义验证方法,也可以在配置文件中通过正则表达式配置验证方法。它不但支持服务器端的验证,客户端的验证也支持,具体需要使用tag把 相应的js方法写入相应的页面中。

配置示例:

Java代码 收藏代码
  1. <form-validation>
  2. <global>
  3. <constant>
  4. <constant-name>验证方法的标志名</constant-name>
  5. <constant-value>正则表达式</constant-value>
  6. </constant>
  7. <validatorname="这个验证方法的标志名,供下面的depends调用"
  8. classname="这个验证方法在哪个类中,为类全名"
  9. method="验证方法的名称"
  10. methodParams="这个验证方法需要的参数类型,依次以逗号格开,为类全名"
  11. depends="基于什么验证之上,可以为多个值,以逗号格开,值为方法的标志名"
  12. jsFunction="js的方法全名,格式为文件路径.方法名。文件路径以点隔开,
  13. 如果不填,默认为org.apache.commons.validator.javascript.xxxx"
  14. msg="对应于properties文件中的一条,作为不通过验证时返回的信息"/>
  15. </global>
  16. <formsetlanguage="语言"country="城市"variant="方言?">
  17. <constant>
  18. <constant-name>验证方法的标志名</constant-name>
  19. <constant-value>正则表达式</constant-value>
  20. </constant>
  21. <formname="bean对象名称">
  22. <fieldproperty="bean中的属性名"depends="需要什么样的验证,可以为多个值,以逗号格开,值为方法的标志名">
  23. <argname="变量名"key="properties文件的key,或者来自Var的name"resource="是/否来自资源文件"/>
  24. <var>
  25. <var-name>变量名</var-name>
  26. <var-value>变量值</var-value>
  27. </var>
  28. </field>
  29. </form>
  30. </formset>
  31. </form-validation>

官方地址:http://commons.apache.org/validator/index.html

参考:http://hi.baidu.com/pengwx/blog/item/db85b84b33d785f183025ce8.html

(2) iScreen


iScreen是一个Java对象验证框架。它的思想与Apache Jakarta的commons-validator项目相似,验证规则使用XML进行配置但也支持其它配置类型。它比commons-validator更强大,灵活,易于使用。

示例:

Java代码 收藏代码
  1. <validation-rootnamespace="my.project">
  2. <validation-setid="RegistrationInfoSet"default-resource="theResource">
  3. <!--First,let'svalidatetheuser'sfirstname.-->
  4. <use-validatorref="org.iscreen.StringValidator">
  5. <mappingfrom="firstName"/>
  6. <labelkey="label.FirstName"/>
  7. <constraintproperty="minLength">1</constraint>
  8. <constraintproperty="maxLength">25</constraint>
  9. </use-validator>
  10. <!--Nowthelastname.-->
  11. <use-validatorref="org.iscreen.StringValidator">
  12. <mappingfrom="lastName"/>
  13. <labelkey="label.LastName"/>
  14. <constraintproperty="minLength">1</constraint>
  15. <constraintproperty="maxLength">30</constraint>
  16. </use-validator>
  17. <!--Makesurethattheregistrationdateisafterthebirthdate.-->
  18. <use-validatorref="org.iscreen.DateRangeValidator">
  19. <mappingfrom="birthDate"to="from"/>
  20. <mappingfrom="registrationDate"to="to"/>
  21. <labelkey="label.RegistrationDate"/>
  22. <failureproperty="failure"key="failure.RegistrationDate"/>
  23. </use-validator>
  24. <!--Checktheemailaddress.-->
  25. <use-validatorref="org.iscreen.StringValidator">
  26. <mappingfrom="emailAddress"/>
  27. <labelkey="label.EmailAddress"/>
  28. <constraintproperty="minLength">1</constraint>
  29. <constraintproperty="maxLength">256</constraint>
  30. </use-validator>
  31. </validation-set>
  32. <resourceid="theResource">
  33. <resource-filefile="messages"/>
  34. </resource>
  35. </validation-root>

官方:http://i-screen.org/docs/index.html

(3) Java对象验证框架OVal


OVal 是一个可扩展的Java对象数据验证框架,验证的规则可以通过配置文件、Annotation、POJOs 进行设定。可以使用纯 Java 语言、JavaScript 、Groovy 、BeanShell 等进行规则的编写。

示例:

Java代码 收藏代码
  1. privatestaticclassTestEntity
  2. {
  3. @Min(1960)
  4. privateintyear=1977;
  5. @Range(min=1,max=12)
  6. privateintmonth=2;
  7. @ValidateWithMethod(methodName="isValidDay",parameterType=int.class)
  8. privateintday=31;
  9. privatebooleanisValidDay(intday)
  10. {
  11. GregorianCalendarcal=newGregorianCalendar();
  12. cal.setLenient(false);
  13. cal.set(GregorianCalendar.YEAR,year);
  14. cal.set(GregorianCalendar.MONTH,month-1);
  15. cal.set(GregorianCalendar.DATE,day);
  16. try{
  17. cal.getTimeInMillis();//throwsIllegalArgumentException
  18. }catch(IllegalArgumentExceptione){
  19. returnfalse;
  20. }
  21. returntrue;
  22. }
  23. }

OVal使用者
The following projects are using OVal:

eSciDoc

https://www.escidoc.org/

SaferJava

http://code.google.com/p/saferjava/

Pinky

https://github.com/pk11/pinky

JProvocateur

http://www.jprovocateur.org/

NexOpen

http://nexopen.sourceforge.net/

gdv.xport

http://repository.agentes.de/gdv/gdv-xport/site/

suz-lab-gae

http://code.google.com/p/suz-lab-gae/

Cubby Simple Web Application Framework

http://cubby.seasar.org/20x/cubby-oval/index.html

Metawidget

http://metawidget.org

Struts 2 OVal Plug-in

http://cwiki.apache.org/confluence/display/S2PLUGINS/OVal+Plugin

Play! Framework

http://www.playframework.org/

Cayenne annotations

http://sourceforge.net/projects/cayannotations/

jsfatwork

http://code.google.com/p/jsfatwork/

mtn4java

http://www.mvnrepository.com/artifact/org.criticalsection.mtn4java/mtn4java/

Polyforms

http://code.google.com/p/polyforms/

rsser

http://code.google.com/p/rsser/

saetc

http://code.google.com/p/saetc/

ultimate-roundtrip

http://code.google.com/p/ultimate-roundtrip/

官方:http://oval.sourceforge.net/

(4) JaValid

JaValid是一个基于标注的验证框架,它允许用户标注Java类来引入验证。JaValid可以应用于任何类型的Java应用程序

示例:

Java代码 收藏代码
  1. packageorg.javalid.examples.core.example01;
  2. importorg.javalid.annotations.core.JvGroup;
  3. importorg.javalid.annotations.core.ValidateDefinition;
  4. importorg.javalid.annotations.validation.MinLength;
  5. importorg.javalid.annotations.validation.NotEmpty;
  6. importorg.javalid.annotations.validation.NotNull;
  7. @ValidateDefinition
  8. publicclassEmployee{
  9. privateStringfirstName;
  10. privateStringlastName;
  11. publicEmployee(){
  12. }
  13. publicvoidsetFirstName(StringfirstName){
  14. this.firstName=firstName;
  15. }
  16. @JvGroup(groups={"group01"})
  17. @NotNull
  18. publicStringgetFirstName(){
  19. returnfirstName;
  20. }
  21. publicvoidsetLastName(StringlastName){
  22. this.lastName=lastName;
  23. }
  24. @JvGroup(groups={"group01"})
  25. @NotEmpty
  26. @MinLength(length=4)
  27. publicStringgetLastName(){
  28. returnlastName;
  29. }
  30. }

Java代码 收藏代码
  1. importjava.util.List;
  2. importorg.javalid.core.AnnotationValidator;
  3. importorg.javalid.core.AnnotationValidatorImpl;
  4. importorg.javalid.core.ValidationMessage;
  5. publicclassTest{
  6. publicTest(){
  7. }
  8. publicstaticvoidmain(String[]args){
  9. AnnotationValidatorvalidator=null;
  10. List<validationmessage>messages=null;
  11. Employeeemp=null;
  12. //Createsadefaultcorevalidatorusingdefaultconfiguration
  13. validator=newAnnotationValidatorImpl();
  14. //Createouremployee,asvalid(noerrorsshouldbefound)
  15. emp=newEmployee();
  16. emp.setFirstName("Martijn");
  17. emp.setLastName("Reuvers");
  18. //Validateouremployee
  19. messages=validator.validateObject(emp,"group01");
  20. System.out.println("Employeeerrors="+messages.size());//Shouldprint0
  21. //Makeouremployeeinvalid
  22. emp.setFirstName(null);//NotNullshouldgetfired
  23. messages=validator.validateObject(emp,"group01");
  24. System.out.println("Employeeerrors="+messages.size());//Shouldprint1
  25. System.out.println("Message="+messages.get(0));//ShouldprintaNotNullmessageerror
  26. //Makeouremployeeevenmoreinvalid
  27. emp.setLastName("");
  28. messages=validator.validateObject(emp,"group01");
  29. System.out.println("Employeeerrors="+messages.size());//Shouldprint2
  30. System.out.println("Messages="+messages);//ShouldprintaNotNull/NotEmptymessageerror
  31. //SetfirstNamefine,lastNametooshortlengthbutnotempty
  32. emp.setFirstName("Martijn");
  33. emp.setLastName("Re");
  34. messages=validator.validateObject(emp,"group01");
  35. System.out.println("Employeeerrors="+messages.size());//Shouldprint1
  36. System.out.println("Messages="+messages);//ShouldprintaMinLengthmessageerror
  37. //Finally,wronggroupspecified,validatesnothing,asitsnotinthe@JvGroupanywhere
  38. messages=validator.validateObject(emp,"invalidGroup");
  39. System.out.println("Employeeerrors="+messages.size());//Shouldprint0
  40. }
  41. }

这个相对功能少些,好像目前不支持配置,纯注解。

官方:http://www.javalid.org/


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值