The structure of the validation. xml file is defined by the validator_1_1_3.dtd file. The content of the file header is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype form-validation public
"-// Apache Software Foundation // DTD commons validator rules configuration 1.1.3 // en"
Http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd>
The top-level element is <form-validation>, which contains <FormSet> and <global> child elements. Generally, you only need to define <FormSet> elements. The <FormSet> Element Definition Format is as follows:
<FormSet>
<Constant> </constant>
<Form> </form>
</FormSet>
<Constant/> is an optional sub-element, while <form/> must appear at least once. <Form/> The Definition Format of child elements is as follows:
<Form name = "">
<Field property = ""> </field>
..............
<Field property = "" depends = "" page = "" indexedlistproperty = ""> </field>
</Form>
The attributes are described as follows:
L name: indicates that the form name is the same as the form-bean name in the configuration file;
L field: The field verified in the form;
L property: the actionform attribute;
L depends: Specify verification rules, such as required and maxlength;
L page: If actionform is a cross-page form, it corresponds to the page attribute in the form.
2. <field> sub-element configuration example
2.1 required (or not empty) Verification
The depends attribute value for non-empty verification is required. The configuration example is as follows:
<Form name = "userform">
<Field property = "loginname" depends = "required">
<Arg0 key = "userform. loginname"/>
</Field>
<Field property = "name" depends = "required">
<Arg0 key = "userform. Name"/>
</Field>
<Field property = "password" depends = "required">
<Arg0 key = "userform. Password"/>
</Field>
</Form>
The key in <arg0 key = "userform. Password"/> corresponds to the key in the resource file.
Note: To generate correct information for the default verification, the developer also needs to add errors to the resource file. default error information such as required. During the operation, the developer can add the following information in the resource file: applicationresources_zh_cn.properties.bak in simplified Chinese:
Errors. Required = {0} cannot be blank
Errors. maxlength = {0} length cannot exceed {1} characters
Errors. minlength = {0} length cannot be less than {1} characters
Errors. Short = {0} must be an integer.
Errors. Integer = {0} must be an integer.
Errors. Long = {0} must be an integer.
Errors. Float = {0} must be a floating point number.
Errors. Double = {0} must be a double-precision type
Errors. Date = {0} must be a date
Errors. range = {0} must be an integer between {1} and {2 }.
Errors. creditcard = {0} must be a valid credit card number
Errors. Email = {0} is an invalid email address.
Add the following content to the English resource file applicaitonresources. properties:
Errors. Required = {0} is required.
Errors. minlength = {0} can not be less than {1} characters.
Errors. maxlength = {0} can not be greater than {1} characters.
Errors. Invalid = {0} is invalid.
Errors. byte = {0} must be a byte.
Errors. Short = {0} must be a short.
Errors. Integer = {0} must be an integer.
Errors. Long = {0} must be a long.
Errors. Float = {0} must be a float.
Errors. Double = {0} must be a double.
Errors. Date = {0} is not a date.
Errors. range = {0} is not in the range {1} through {2 }.
Errors. creditcard = {0} is an invalid credit card number.
Errors. Email = {0} is an invalid e-mail address.
2.2 max length and min length Verification
You can add maxlength and minlength to the depends attribute of the <field> element to limit the maximum and minimum length of the field. The example is as follows:
<Field property = "password" depends = "required, maxlength, minlength">
<Arg0 key = "userform. Password"/>
<Var>
<Var-Name> maxlength </var-Name>
<Var-value> 12 </var-value>
</Var>
<Var>
<Var-Name> minlength </var-Name>
<Var-value> 6 </var-value>
</Var>
</Field>
2.3 email Verification
You can also add email verification in struts. In this case, depends is email. The example is as follows:
<Field property = "email" depends = "required, email">
<Arg0 key = "userform. Email"/>
</Field>
2.4 date Verification
You can also add date verification in struts. In this case, depends is date. The example is as follows:
<Field property = "begintime" depends = "date">
<Arg0 key = "historyform. begintime"/>
</Field>
2.5 Integer Range Verification
In struts, you can also verify whether an integer is in a range. For example, the following example shows whether the integer is between 1 and 9999. The configuration is as follows:
<Field property = "score" depends = "intrange">
<Arg0 key = "testform. Score"/>
<Arg1 name = "intrange" Key = "$ {var: min}" resource = "false"/>
<Var>
<Var-Name> min </var-Name>
<Var-value> 1 </var-value>
</Var>
<Arg2 name = "intrange" Key = "$ {var: max}" resource = "false"/>
<Var>
<Var-Name> max </var-Name>
<Var-value> 99999 </var-value>
</Var>
</Field>
2.6 custom Verification
The validation mechanism in Struts provides extensions. developers can add custom verification rules, such as file type verification. The Java class that needs to be written at this time, the JS that adds the validation rule in the validator-rules.xml, And the Add validation.
Custom verification Java class mmvalidatorCodeAs follows:
Package com. Amigo. Struts. validation;
Import java. util. RegEx. matcher;
Import java. util. RegEx. pattern;
Import javax. servlet. http. httpservletrequest;
Import org. Apache. commons. validator. field;
Import org. Apache. commons. validator. genericvalidator;
Import org. Apache. commons. validator. validator;
Import org. Apache. commons. validator. validattion tion;
Import org. Apache. commons. validator. util. validatorutils;
Import org. Apache. Struts. Action. actionmessages;
Import org. Apache. Struts. validator. Resources;
/**
* Struts custom verification class.
*/
Public class customvalidator {
/**
* Determine the file type
*/
Public static Boolean validatefiletype (Object bean, validatemedition va,
Field field, actionmessages errors, validator,
Httpservletrequest request ){
String value = validatorutils. getvalueasstring (bean, Field
. Getproperty ());
String inputtype = value. substring (value. lastindexof ('.'));
String type [] = field. getvarvalue ("filetypeproperty"). Split (";");
If (! Genericvalidator. isblankornull (value )){
Try {
Boolean judge = false;
For (INT I = 0; I <type. length; I ++ ){
Pattern P = pattern. Compile (type [I],
Pattern. case_insensitive );
Matcher M = P. matcher (inputtype );
Judge = M. Matches ();
If (judge ){
Break;
}
}
If (! Judge ){
Errors. Add (field. getkey (), resources. getactionmessage (
Validator, request, VA, field ));
Return false;
}
} Catch (exception e ){
Errors. Add (field. getkey (), resources. getactionmessage (
Validator, request, VA, field ));
Return false;
}
}
Return true;
}
}
Then you also need to add the following content to the validator-rules.xml validation rule file:
<! -- Determine the file type -->
<Validator name = "filetype" classname = "com. Cotel. Comm. earlyvalidator" method = "validatefiletype"
Methodparams = "Java. Lang. object,
Org. Apache. commons. validator. validatemedition,
Org. Apache. commons. validator. Field,
Org. Apache. Struts. Action. actionmessages,
Org. Apache. commons. validator. validator,
Javax. servlet. http. httpservletrequest"
MSG = "errors. filetype">
<JavaScript>
<! [CDATA [
Function validatefiletype (form ){
VaR isvalid = true;
VaR focusfield = NULL;
VaR I = 0;
VaR fields = new array ();
VaR formname = form. getattributenode ("name ");
Ofiletype = eval ('new' + formname. Value + '_ filetype ()');
For (X in ofiletype ){
VaR field = form [ofiletype [x] [0];
If (field. type = 'ddn' |
Field. type = 'text' |
Field. type = 'file' |
Field. type = 'textea ')&&
Field. Disabled = false ){
VaR IMAX = ofiletype [x] [2] ("filetypeproperty"). Split (";");
VaR Index = field. value. lastindexof (".");
VaR length = field. value. length;
VaR filetype = field. value. substring (index, length );
VaR judege = false;
If (length> 0 & filetype! = ""){
For (var j = 0; j <Imax. length; j ++ ){
If (IMAX [J]. tolowercase () = filetype. tolowercase ()){
Judege = true;
Break;
}
}
If (! Judege ){
If (I = 0 ){
Focusfield = field;
}
Fields [I ++] = ofiletype [x] [1];
Isvalid = false;
}
}
}
}
If (fields. length> 0 ){
Focusfield. Focus ();
Alert (fields. Join ('"n '));
}
Return isvalid;
}
]>
</JavaScript>
</Validator>
The preceding custom verification can be used to verify the file type. In the following example, the file type is verified as JPG. For example:
<Field property = "picpath" depends = "required, filetype">
<Arg0 key = "testform. picpath"/>
<Var>
<Var-Name> filetypeproperty </var-Name>
<Var-value>. jpg </var-value>
</Var>
</Field>
3. Complete instance Configuration
This section provides a complete example of the validation. xml configuration. This example summarizes and adds verification rules for the two forms. The content is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype form-validation public
"-// Apache Software Foundation // DTD commons validator rules configuration 1.1.3 // en"
Http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd>
<Form-validation>
<FormSet>
<Form name = "userinfoform">
<Field property = "email" depends = "email">
<Arg0 key = "userinfoform. Email"/>
</Field>
<Field property = "mobile" depends = "required">
<Arg0 key = "userinfoform. Mobile"/>
</Field>
<Field property = "Description" depends = "maxlength">
<Arg0 key = "userinfoform. Description"/>
<Arg1 name = "maxlength" Key = "$ {var: maxlength}" resource = "false"/>
<Var>
<Var-Name> maxlength </var-Name>
<Var-value> 200 </var-value>
</Var>
</Field>
</Form>
<Form name = "passwordform">
<Field property = "newpass" depends = "required">
<Arg0 key = "passwordform. newpass"/>
</Field>
<Field property = "oldpass" depends = "required">
<Arg0 key = "passwordform. oldpass"/>
</Field>
<Field property = "conpass" depends = "required">
<Arg0 key = "passwordform. conpass"/>
</Field>
</Form>
</FormSet>
</Form-validation>