Struts 1 validator framework learning notes

Source: Internet
Author: User

Why is it difficult to use the validate () method of actionform to verify user input data before validator is available?

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'sValidate ()Method, with validator you use an XML
Configuration file to declare the validations that shoshould 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 valim validations into validator. (customize your validation .)

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

Conclusion: The validator framework uses validator'sActionformReplace the original data in actionformReset inAnd Master Verification Method

 

 

With validator, we can:

1.
Not Write validate () method
Simply by extending a class from validator'sActionformSubclasses;

2.
Plug or remove validator's
Framework in our struts application;

3. UseValidator-rules.xmlFile 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 & validatemeditionform
.

Validatexceptionform is used to solve the problem that two different actions must perform different verification on the same form. The implementation method is as follows:Validation. xmlVerify MappingYou do not need form name,And use action pathTo replace, for example:

 

<formset>
<Form name = "/createaddress">Logical form name is not used here
    <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 &:

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

2. What
About lazyformbean? How does it coordinate with the validator framework?

3. How
Is the formbean referred inValidation. xmlMapped toValidator-rules.xml?

A.Validator uses the value ofFormTag's
NameAttribute to match validation definitions to the name of the form
Bean to which they are applied.


B.
InValidation. xml fileThere is
<Depend = ""> to specify the field to which routine defined inValidator-rules.xml
Is used.

 

 

)

 

Note: to override the validate () method of actionform, you must call the super. Validate () method of the parent class.


AboutValidator-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
Perform a specific validation.Validator-rules.xmlFile is used
Declaratively plug in the validation routines that validator will use
Parameter Ming validations. Struts comes packaged with a preconfigured copy of this
File in the struts Core. JarFile (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 wocould require extracting it
From the core. JarFile, making changes to the file and then repackaging
The core. JarFile with the modified file. As you can imagine, that is
Cumbersome and shocould only be done if absolutely necessary. Otherwise you can
Simply add validation routine definitions toValidation. xmlFile
Explained in the section "creating custom validations ."

The validator framework is designed as a pluggable verification system. Some Java methods are inserted into this system to perform some validation operations.Validator-rules.xmlDefines a set of such common paths, which are automatically packaged in strutsIn the kernel package, this file generally does not need to be modified. If you need a custom authentication operation, that is, in the Custom validation. xmlWrite can be; (validator-rules.xml
The verification process and JavaThe method is very similar, specifying a class and its methods, parameters and messages, which is to understand the validator-rules.xmlThe key to syntax.)

 

Example:

 

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

 

Name of the validator tag
Attribute, indicating the logical name of a routine.

Notice thatValidatorTag specifies
MSGAttribute.MSGAttribute 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 thatValidatorTag specifies
AJsfunctionAttribute.JsfunctionAttribute is used
Define the path to a file that contains client-side JavaScript code for
Validation routine. The javascript code performs the same validation on
Client Side as is stored med on the server side.

MSG indicates a message.
Message defined in resource bundle file (Application resource file)

 

 

 

Application
Resource bundle File

Validator uses the struts resource bundle
Mechanisms for externalizing error messages. (This mechanism is used to save error information. When an error occurs in the verification data, the error information is displayed to the user from these files)

 

 


Processing ing
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 usesName
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<Form>
Element,<Field>Elements are used to define the validations that
Will be applied to specified form bean fields .(<Field>The tag defines the attributes of the form to be verified.)

The<Field>Element'sProperty
Attribute corresponds to the name of a field in the specified form bean.

TheDependsAttribute specifies
Logical names of validation routines fromValidatorrules. xmlFile
That shoshould be applied to the field. The validations specified withDepends
Attribute will be specified med in the order specified and they all must pass (depends ingValidator-rules.xmlA routine.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.