Struts 1 Validator框架学习笔记

来源:互联网 发布:淘宝商品拍摄价格 编辑:程序博客网 时间:2024/05/22 03:49

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

1. The Validator framework comesprepackaged with several validation routines, making the transition fromhard-coded validation logic painless. (What ishard-coded validation logic? Is it that programmers do not have to write codeline by line to perform validation?)  Instead of coding validation logic in eachForm Bean's validate( ) method, with Validator you use an XMLconfiguration file to declare the validations that should be applied to eachForm Bean(configure the XML once and changeeverywhere? All validation integrated in XML file. )

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

3. Validator supports both server-sideand client-side (JavaScript) validations whereas Form Beans only provide aserver-side validation interface. (JavaScriptValidation supported. How?)

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

 

 

With validator, we can:

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

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

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

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

 

 

2ways of creating Form bean: 

  a. createa concrete Form Bean object:  publicclass LogonForm extends ValidatorForm

  b.use DynaValidatorForm:

  <form-bean name="logonForm"

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

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

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

  </form-bean>

 

Thereare 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.  DoI need to specify the property of the form bean in validation.xml? ( Y)

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

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

     a.  Validator uses the value of the form tag'sname attribute to match validation definitions to the name of the FormBean 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.xmlis used.

 

 

)

 

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


关于validator-rules.xml

 

TheValidator framework is set up as a pluggable system whereby each of itsvalidation routines is simply a Java method that is plugged into the system toperform a specific validation. The validator-rules.xml file is used todeclaratively plug in the validation routines that Validator will use forperforming validations. Struts comes packaged with a preconfigured copy of thisfile 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 notever need to modify it. Modification to the file would require extracting itfrom the core .jar file, making changes to the file and then repackagingthe core .jar file with the modified file. As you can imagine, that iscumbersome and should only be done if absolutely necessary. Otherwise you cansimply add validation routine definitions to the validation.xml file asexplained 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 amsg attribute. The msg attribute specifies a key for a message inthe application resource bundle file that will be used as the error messagewhen the validation fails. Notice also that the validator tag specifiesa jsFunction attribute. The jsFunction attribute is used todefine the path to a file that contains client-side JavaScript code for thevalidation routine. The JavaScript code performs the same validation on theclient side as is performed on the server side.

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

 

 

 

ApplicationResource Bundle File

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

 

 


Configuringvalidation.xml

form-validation>

  <formset>

    <formname="logonForm">

      <fieldproperty="username" depends="required">

        <argposition="0" key="prompt.username"/>

     </field>

      <fieldproperty="password" depends="required">

        <argposition="0" key="prompt.password"/>

     </field>

    </form>

  </formset>

</form-validation>

 

Each <form> element uses the nameattribute 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 definedin the Struts configuration file. Based on the type of Form Bean beingvalidated, Validator will attempt to match the name either against a FormBean's logical name or against an action's path. Inside the <form>element, <field> elements are used to define the validations thatwill be applied to specified Form Bean fields. <field>标签定义了要验证的表单的属性

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

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

原创粉丝点击