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.)

 

Arg 是用来干什么的?

arg 即是定义错误信息显示的!

 

Working with Configurable Validations(使用可配置的验证路径)

Each of the prepackaged validations provided by Validator requires configuring before it will function properly.(每个在包里Validator预定义的路径必须先配置才可以工作正常。) There are two types of configuration constructs that validations can use: error message parametric replacement definitions and variable definitions. (有两种验证构造方式, parametric replacement definitions, 参数替换, 即自己定义些消息部分传过去,打印错误信息时则会个性化地显示错误信息。换言之, Validator框架是提供了一些错误消息模板,自己要做的是把个性错误信息部分补上,即是arg部分要做的事。

Error message parametric replacement definitions are defined with the arg tag and specify values for placeholders in the error message associated with a validation.

 

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

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

</field>

The key attribute of the arg tag specifies a key for a value in the application resource bundle file that will be used to populate the validation's error message(key属性用于确定一个关键值,这个关键值在application resource bundle file 确定对应一个error message )

Alternatively, you can set the resource attribute of the arg tag to "false" to indicate that the value specified with the key attribute should be taken as the literal replacement value instead of as the key for a value in the resource bundle 
<field property="password" depends="required, minlength">
  <arg position="0" key="Password" resource="false"/>
</field>

The value "Password" will be used to replace the "{0}" in the validations' error messages

(当把属性resource 设置成 false以后, key 的值不再是对应application resource bundle file的一个消息,而是直接更换错误消息,按照我的理解,上述的例子的错误消息应是: Password is required)

 

Using Validator's Included Validations

 

Enabling Client-Side Validations(激活客户端的验证)

 

To enable client-side validation, you have to place the HTML Tag Library's javascript tag in each JSP for which you want client-side validation performed, as shown here:

<html:javascript formName="logonForm"/>

(必须要在JSP文件里声明哪个表单要用到javascript验证)

 

 

<html:form action="/Logon" οnsubmit="return validateLogonForm(this);">


使用自定义validation的步骤:

1. 创建Form,(继承ValidatorForm即可)

2. 创建validation.xml, 增加规则及映射对应的formvalidator (自定义的validator类可以是通用的)

3. struts-config.xml file里加入validator插件说明

4. MessageResources.properties加入错误信息

5.  OK give me a five!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: BootstrapValidator是一个基于Bootstrap框架的jQuery表单验证插件。它可以用来验证各种类型的表单元素,如文本框、下拉框、单选按钮等。它支持多种验证方式,如长度限制、数字范围、邮箱格式等。使用BootstrapValidator可以轻松地在表单中添加验证功能,提高用户体验。 ### 回答2: bootstrapvalidator是一个基于Bootstrap框架的表单验证插件。它通过添加各种验证规则和选项,能够为表单提供强大的实时验证功能。 使用bootstrapvalidator可以轻松地对用户的输入进行验证,确保数据的准确性和完整性。它支持各种验证类型,如必填字段验证、长度验证、数字验证、邮箱验证、密码强度验证等等。你只需简单地在表单元素上添加相应的验证规则,bootstrapvalidator就会自动实时验证输入的数据,并在错误发生时提供错误消息。 通过bootstrapvalidator的配置选项,你可以根据需要定制验证的方式和提示信息。它提供了丰富的回调函数,可以在验证通过或验证失败时执行相应的操作。你可以用自定义的错误消息替换默认的提示信息,或者修改验证的样式以适应你的网站风格。 bootstrapvalidator的使用非常简便,只需在页面中引入相关的CSS和JavaScript文件,并在表单元素上添加相应的class和data属性即可。其官方文档提供了详细的使用说明和示例代码,帮助你快速上手。 总而言之,bootstrapvalidator是一个功能强大、灵活易用的表单验证插件。它可以帮助开发者简化表单验证的过程,提高用户输入数据的准确性和完整性。无论是开发响应式网站还是移动应用,bootstrapvalidator都是一个不错的选择。 ### 回答3: BootstrapValidator是一个基于Bootstrap的表单验证插件。它提供了一套简单易用的验证规则和方法,可以方便地对表单进行前端验证。使用BootstrapValidator可以减少后端的验证压力,提高用户体验。 BootstrapValidator支持多种常见的验证规则,比如必填字段、电子邮件格式、URL格式、数字范围等等。通过设置相应的选项,可以定义验证规则,并在表单提交之前对数据进行验证。验证结果会实时显示在表单中,如果数据不符合规则,会给出相应的错误提示。 使用BootstrapValidator,可以灵活地自定义验证规则和错误提示信息。可以根据需要,自定义正则表达式验证规则,或者自定义提示信息的显示方式。 BootstrapValidator还支持在表单提交之前进行远程验证。可以通过AJAX请求向后端发送数据,并根据返回结果判断数据是否合法。 总的来说,BootstrapValidator是一个简单、易用且功能强大的表单验证插件。它可以减少开发者在后端处理表单验证的工作量,提高用户体验。使用BootstrapValidator,可以轻松地实现表单验证功能,保证数据的准确性和完整性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值