Struts 1 Validator框架学习笔记

为什么在没有Validator之前,用Actionformvalidate()方法去验证用户输入的数据的方式不好?

1.  The Validator framework comes prepackaged with several validation routines, making the transition from hard-coded validation logic painless. (What is hard-coded validation logic? Is it that programmers do not have to write code line by line to perform validation?)  Instead of coding validation logic in each Form Bean's validate( ) method, with Validator you use an XML configuration file to declare the validations that should be applied to each Form Bean(configure the XML once and change everywhere? All validation integrated in XML file. )

2.  If you need a validation not provided by Validator, you can plug your own custom validations into Validator.(Customize your validation.)

3.  Validator supports both server-side and client-side (JavaScript) validations whereas Form Beans only provide a server-side validation interface. (JavaScript Validation supported. How?)

总结: Validator框架就是用Validator's ActionForm去代替原来放在 ActionForm里的reset 和验主方法

 

 

With validator, we can:

1.       not write Validate() method simply by extending a class from Validator's ActionForm subclasses;  

2.       plug or remove Validator’s framework in our Struts application;

3.       use the validator-rules.xml file to define client-side JavaScript code (or the location of client-side JavaScript code) for each validation routine;

4.       use the second configuration file, validation.xml, to define which validation routines are applied to which Form Beans;

 

 

2 ways of creating Form bean: 

  a. create a concrete Form Bean object:  public class LogonForm extends ValidatorForm

  b. use DynaValidatorForm:

  <form-bean name="logonForm"

             type="org.apache.struts.validator.DynaValidatorForm">

    <form-property name="username" type="java.lang.String"/>

    <form-property name="password" type="java.lang.String"/>

  </form-bean>

 

There are differences between ValidatorForm & ValidatorActionForm.

ValidatorActionForm是用于解决两个不同的Action要对同一个Form做不同的验证时所要做的,实现的方法是,在validation.xml里验证mapping时,不用 Form name, 而用action path来代替,例:

 

<formset>
  <form name="/createAddress">           此处便是不用logical form name
    <field property="city" depends="required">
      <arg position="0" key="prompt.city"/>
    </field>
  </form>
  <form name="/editAddress">
    <field property="state" depends="required">
      <arg position="0" key="prompt.state"/>
    </field>
  </form>
</formset>

 

 

 

(

Q&A:

1.  Do I need to specify the property of the form bean in validation.xml? ( Y)

2.  What about lazyFormBean? How does it coordinate with the Validator Framework?

3.  How is the formbean referred in the validation.xml mapped to the validator-rules.xml?

     a.  Validator uses the value of the form tag's name attribute to match validation definitions to the name of the Form Bean to which they are applied.

   b.  In the validation.xml file there is a <depend= “”> to specify the field to which routine defined in validator-rules.xml is used.

 

 

)

 

注: 当需要重写ActionFormvalidate()方法时,必须调用父类的super.validate()方法。


关于validator-rules.xml

 

The Validator framework is set up as a pluggable system whereby each of its validation routines is simply a Java method that is plugged into the system to perform a specific validation. The validator-rules.xml file is used to declaratively plug in the validation routines that Validator will use for performing validations. Struts comes packaged with a preconfigured copy of this file in the Struts core .jar file (e.g., struts-core-1.3.5.jar). Under most circumstances, you will use this preconfigured copy and will not ever need to modify it. Modification to the file would require extracting it from the core .jar file, making changes to the file and then repackaging the core .jar file with the modified file. As you can imagine, that is cumbersome and should only be done if absolutely necessary. Otherwise you can simply add validation routine definitions to the validation.xml file as explained in the section "Creating Custom Validations."

Validator框架就是设计成一个可插入的验证体系,一些java方法主插入这个体系里去做某些验证性的操作。validator-rules.xml 就定义了这样一些常规路径的集合,它自动包在struts内核包里,一般这个文件不需要做什么改动;如果需要自定义验证操作,即在自定义的validation.xml写上皆可;(validator-rules.xml 定义的验证过程跟java方法很像,指定了某个类及其中的方法,参数及消息,这是理解validator-rules.xml语法的关键。

 

例:

 

<form-validation>
  <global>
    <validator name="minlength"
          classname="org.apache.struts.validator.FieldChecks"
             method="validateMinLength"
       methodParams="java.lang.Object,
                     org.apache.commons.validator.ValidatorAction,
                     org.apache.commons.validator.Field,
                     org.apache.struts.action.ActionMessages,
                     org.apache.commons.validator.Validator,
                     javax.servlet.http.HttpServletRequest"
                msg="errors.minlength"
         jsFunction="org.apache.commons.validator.javascript.validateMinLength"/>
  </global>

 

Validator 标签的name 属性,表示一个routinelogical name.

Notice that the validator tag specifies a msg attribute. The msg attribute specifies a key for a message in the application resource bundle file that will be used as the error message when the validation fails. Notice also that the validator tag specifies a jsFunction attribute. The jsFunction attribute is used to define the path to a file that contains client-side JavaScript code for the validation routine. The JavaScript code performs the same validation on the client side as is performed on the server side.

Msg 表示一个消息,在application resource bundle file(应用程序资源文件)里定义的消息

 

 

 

Application Resource Bundle File

Validator uses the Struts Resource Bundle mechanism for externalizing error messages.(这个机制是用来保存错误信息的,当验证数据出错时,就从这些文件里调出错误信息显示给用户)

 

 


Configuring validation.xml

form-validation>

  <formset>

    <form name="logonForm">

      <field property="username" depends="required">

        <arg position="0" key="prompt.username"/>

      </field>

      <field property="password" depends="required">

        <arg position="0" key="prompt.password"/>

      </field>

    </form>

  </formset>

</form-validation>

 

Each <form> element uses the name attribute to associate a name with the set of field validations it encompasses. Validator uses this logical name to map the validations to a Form Bean defined in the Struts configuration file. Based on the type of Form Bean being validated, Validator will attempt to match the name either against a Form Bean's logical name or against an action's path. Inside the <form> element, <field> elements are used to define the validations that will be applied to specified Form Bean fields. <field>标签定义了要验证的表单的属性

The <field> element's property attribute corresponds to the name of a field in the specified Form Bean.

The depends attribute specifies the logical names of validation routines from the validatorrules.xml file that should be applied to the field. The validations specified with the depends attribute will be performed in the order specified and they all must pass(depends即映射到validator-rules.xml里的某个routine.)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值