Struts-validator verification framework.

Source: Internet
Author: User
Tags valid email address

I learned validator today. One method in actionform is validate (), which can be used to write verification code. But the disadvantage is that if I have a lot of actionforms, I don't want to write them to the dead. Therefore, the dynamic form (dynaactionform) appears, but it must be verified based on the dynamic form. This is troublesome.

Validator was created by David winterfeldt and can be integrated into the Struts framework. Verifies data. In fact, you do not need to write the verification code. That is, there are many configurations.

Let's talk about the benefits of using validator.

1. A complete set of verification rules can be defined for the program at one place;

2. It is easier to modify or define a new verification.

3. Support Internationalization

4. Regular Expressions supported

5. It can be applied to web programs or Java applications;

6. validator supports bability)

Verification Framework validatorframewordk
The principle is to fix some common verification rules (validator-rules.xml)
The configuration of the XML file (validation. XML) is used to specify which actioniform attribute uses the verification rules.

Steps:

1. Configure the struts-config.xml file so that the Struts framework can recognize the verification framework
2. Create a dynamic form and change it to the dynavalidatorform class. This class uses the verification framework for verification.
3. Write the validation. xml file and configure your own validation rules.

 

 

The following is my job:

It is easy to verify. Field username, password, age, email

Then verify the fields.

First configure the struts-config.xml to let struts understand your framework

 <Plug-in classname = "org. apache. struts. validator. validatorplugin "> <br/> <set-Property =" pathnames "value ="/WEB-INF/validator-rules.xml, <br/>/WEB-INF/validation. XML "/> <br/> </plug-in>

 

2. Create a dynamic form

<Form-bean name = "reguserform" type = "org. apache. struts. validator. dynavalidatorform "> <br/> <form-property name =" username "type =" Java. lang. string "/> <br/> <form-property name =" userpwd "type =" Java. lang. string "/> <br/> <form-property name =" userage "type =" Java. lang. string "/> <br/> <form-property name =" useremail "type =" Java. lang. string "/> <br/> <form-property name =" userpwds "type =" Java. lang. string "/> <br/> </form-bean>

Note org. Apache. Struts. validator. dynavalidatorform

3 create 3 validation. xml under WEB-INF

 

<Form-validation> </P> <p> <FormSet> <br/> <form name = "reguserform"> <br/> <field property = "username" depends = "required"> <br/> <Arg name = "required" position = "0" resource = "true" Key = "username"> </Arg> <br/> </field> </P> <p> <field property = "userpwd" depends = "required, minlength, maxlength "> <br/> <Arg name =" required "position =" 0 "resource =" true "Key =" userpwd "> </Arg> <br/> <ARG name = "minlength" position = "1" resource = "false" Key = "6"> </Arg> <br/> <Arg name = "maxlength" position = "0 "resource =" true "Key =" userpwd "> </Arg> <br/> <Arg name =" maxlength "position =" 1 "resource =" false "Key =" 8 "> </Arg> <br/> <var-Name> minlength </var-Name> <br/> <var-value> 6 </var-value> <br/> </var> <br/> <var-Name> maxlength </var-Name> <br/> <var-value> 8 </var-value> <br/> </var> <br/> </field> <br/> <field property = "userpwds" depends = "validwhen"> <br/> <MSG name = "validwhen" resource = "true" Key = "userpwds"/> <br/> <var> <br/> <var-Name> test </var-Name> <br/> <var-value> (userpwds = userpwd) </var-value> <br/> </var> <br/> </field> <br/> <field property = "userage" depends = "intrange"> <br/> <Arg name = "intrange" position = "0" Key = "userage"> </Arg> <br/> <Arg name = "intrange" position = "1 "resource =" false "Key =" $ {var: min} "> </Arg> <br/> <Arg name =" intrange "position =" 2 "resource =" false "Key =" $ {var: max} "> </Arg> <br/> <var-Name> min </var-Name> <br/> <var-value> 18 </var-value> <br/> </var> <br/> <var-Name> max </var-Name> <br /> <var-value> 35 </var-value> <br/> </var> <br/> </field> <br/> <field property = "useremail "depends =" email, required "> <br/> <Arg name =" required "position =" 0 "resource =" true "Key =" email "> </Arg> <br/> <ARG name = "email" position = "0" Key = "email" resource = "true"> </Arg> <br/> </field> <br/> </Form> <br/> </FormSet> <br/> </form-validation>

 

Validator-rules.xml files

The validator-rules.xml file defines the validator that an application can use. The validator-rules.xml acts as a template that defines the validator that all applications may need.

Next, let's explain the elements in it.

Validator has 14 Rules by default. This write rule is called the basic rule of the validator framework.

Name Description
Byte, short, integer, long, float, double Test whether the value can be converted to the corresponding basic data type
Creditcard Check whether the input domain is a valid credit card number.
Date Checks whether the input field is a valid date.
Email Check whether the input is a valid email address.
Mask Checks whether the input field matches a regular expression.
Maxlength Whether the length of the test value is smaller than or equal to the given maximum length
Minlength Whether the length of the test value is greater than or equal to the given minimum length
Range Check whether the value range is between the maximum and minimum values.
Required Check whether the input field is not empty or whether the length of the Space value is greater than zero

 

 

 

1 FormSet

 

Language and country attributes are available

Element constant and form

 

2 form

The name with an attribute name must be the same as your dynavdlidtorform name.

Element Field

 

3 Field

The value of the attribute depends corresponds to"Validator-rules.xml files".

Property corresponds to the name of the property in the form dynavdlidtorform.

Element Arg msg

 

4 ARG

The depends key corresponding to an attribute name corresponds to the message text, that is, the key in the resource file.

Resource indicates whether to find the corresponding key from the resource file.

Position corresponds to the Sequence Value in the resource file

 

5 msg

 

The depends key corresponding to an attribute name corresponds to the message text, that is, the key in the resource file.

The depends key corresponding to an attribute name corresponds to the message text, that is, the key in the resource file.

The default resource value is true.

Position corresponds to the Sequence Value in the resource file

 

6 VaR

 

There are two elements. One is the value of the VaR-name var-value parameter. Or rule expression

 

 

Framework client verification (JavaScript) Page

  1. <HTML: javascript formname = "registform"/>
  2. <HTML: Form Action = "regist. Do" onsubmit = "Return validateregistform (this)">

But the depressing thing is that the page goes out on a daily basis.

 

 

 

All are prompts in English. Obviously. Message prompts in the resource file are in English. In this case, convert these messages to UTF-8.

 

Use the built-in JDK transcoding tool native2ascii.exe and open it and enter the Chinese name you want to prompt for transcoding.

  1. Errors. Required = {0}/u4e0d/u80fd/u4e3a/u7a7a
  2. Errors. minlength = {0}/u4e0d/u80fd/u5c11/u4e8e {1}/u4e2a/u5b57/u7b26

However, transcoding is troublesome. Of course, you can try the second method to transcode the entire resource file.

 

Enter cmd to enter the directory drive letter e of your resource file: Then open the path of the CD resource file and enter native2ascii.exe encoding UTF-8 Resource Name alias after transcoding is successful as follows:
  1. Errors. maxlength = {0}/u4e0d/u80fd/u5927/u4e8e {1}/u4e2a/u5b57/u7b26
  2. Errors. Invalid = {0}/u662f/u975e/u6cd5/u7684
  3. Errors. validwhen = {0}/u4e0e {1}/u4e0d/u5339/u914d
  4. Errors. Date = {0}/u4e0d/u7b26/u5408/u65e5/u671f/u683c/u5f0f
  5. Errors. Email = {0}/u4e0d/u662f/u5408/u6cd5/u7684/u7535/u5b50/u90ae/u7bb1
  6. EMP. Username =/u7528/u6237/u540d
  7. EMP. Password =/u5bc6/u7801
  8. EMP. repassword =/u786e/u8ba4/u5bc6/u7801
  9. EMP. Birthday =/u51fa/u751f/u65e5/u671f
  10. EMP. Email =/u7535/u5b50/u90ae/u7bb1
  11. EMP. Mobile =/u624b/u673a/u53f7

It's over. Submit the job.

 

 

 

 

 

 

 

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.