Spring's validate verification requires two beans (validatorfactory, beanvalidator) and two XML files (validator-rules.xml, validation. XML) support, validatorfactory is used to manufacture beanvalidator, the latter is the control bean that executes validation in the program. Validatorrules. xml defines various validation rules. For example, the field is not empty, and the field input value must be an integer. Validation. xml defines which attribute fields in the beans and beans need to be verified and which verification rules are used. Validate
There are two levels of verification: client-side verification (JavaScript) and server-side verification. To use validate, follow these steps:
1. Declare validatorfactory and beanvalidator in the configuration file:
<Bean id = "validatorfactory" class = "org. springmodules. commons. validator. defaultvalidatorfactory"> ----- ①
<Property name = "validationconfiglocations"> ----- ②
<List>
<Value>/WEB-INF/validator-rules.xml </value>
<Value>/WEB-INF/validation. xml </value>
</List>
</Property>
</Bean>
<Bean id = "beanvalidator"
Class = "org. springmodules. commons. validator. defaultbeanvalidator"> ------- ③
<Property name = "validatorfactory">
<Ref bean = "validatorfactory"/> -------- ④
</Property>
</Bean>
① Declare validatofactory here we use spring's defaultvalidatorfactory
② Define its validationconfiglocations attribute, pass the validator-rules.xml and validation. xml
③ Declare beanvalidator. Here we use spring's defaultbeanvalidator.
④ Specify the validatorfactory to be used in its properties. Here we use the validatorfactory just defined
2. Declare validate in the Controller to be verified (that is, the Controller of the form or command.
<Bean id = "xxxxcontroller"……>
<Property name = "commandname" value = "usercommand"/> ----- ①
<Property name = "commandclass" value = "com. resoft. User"/> ----- ②
<Property name = "validator" ref = "beanvalidator"/> ------- ③
......
</Bean>
① Commandname indicates the name of the command to be verified. The name must be the same as the name written in <form name = "XXXXX"> in validation. xml.
② Commandclass is used to specify the command type. It must be consistent with the command type formed at the end of the form submitted by your JSP.
③ Declare that the controller uses validator. Here we pass in the beanvalidator defined.
3. In the validation. xml file, define the formbean (or command) You want to verify, and define the fields in the bean that need verification and the rules used for verification. (Note: The form name defined here must be consistent with the commandname defined in the previous controller.) below are several common verification examples:
<Form-validation>
<FormSet>
<Form name = "usercommand"> ----- ①
<Field property = "userid" depends = "required"> ----- ②
<Arg0 key = "User code"/> ------- ③
</Field>
① The Name Of The bean to be verified is defined here, And the commandname defined above must be the same
② Define the bean attribute to be verified as userid and use the rule as required (not empty)
③ The display information is defined here. arg0 indicates the display information of the position 0 and the information indicated by the key (if you have more information, use arg1, arg2, and so on) the error message is displayed as follows: the user code cannot be blank.
<Field property = "Age" depends = "required, integer, mask"> ----- ①
<Arg0 key = "Age"/>
<MSG name = "Mask" Key = "errors. Negative"/> ----- ②
<Var>
<Var-Name> mask </var-Name>
<Var-value> ^ [1-9] </var-value> ------- ③
</Var>
</Field>
① Use a regular expression to constrain the input value of the form for mask tag verification. (For example, only numbers, letters, or specified characters can be entered)
② The MSG label is used to define the association between verification rules and error information. Here, the information specified by errors. Negative is displayed when the Mask Verification fails.
③ The entire <var> label is used to define specific rules. For example, <var-Name> mask </var-Name> indicates that it is a mask rule, <var-value> ^ [1-9] </var-value> indicates that only the numbers starting with 1-9 are allowed.
Therefore, the above verification information is that the age field cannot be blank, must be an integer, and must start with 1-9 to eliminate negative and zero.
<Field property = "Birthday" depends = "required, Date"> ----- ①
<Arg0 key = "Birthday"/>
<Var>
<Var-Name> datepatternstrict </var-Name> ----- ②
<Var-value> yyyy-mm-DD </var-value> ----- ③
</Var>
</Field>
</FormSet>
① Use "date" to add validation of the date format
② Start to define the date format. var-name must be written as datepatternstrict
③ Define the desired date format, such as yyyy-mm-dd yy/MM/DD.
<Field property = "startdate" depends = "required, Date">
<Arg0 key = "Start Date"/>
<Var>
<Var-Name> datepatternstrict </var-Name>
<Var-value> yyyy-mm-DD </var-value>
</Var>
</Field>
<Field property = "enddate" depends = "required, date, comparetwofield"> ----- ①
<Arg0 key = "End Date"/>
<Arg1 key = "Start Date"/> ----- ②
<Var>
<Var-Name> datepatternstrict </var-Name>
<Var-value> yyyy-mm-DD </var-value>
</Var>
<Var>
<Var-Name> secondfield </var-Name> ------- ③
<Var-value> startdate </var-value>
</Var>
</Field>
① Here we demonstrate the verification of two dates. To reach the start date, the end date cannot be later than the end date, add the comparetwofield verification rule.
② Define 2nd display parameters: "Start Date" of AGR1"
③ Add a var whose var-name is secondfield (this is written to death in the program and must be written to secondfield)
④ Var-value is defined as the attribute name of the start date, for example, startdate in this example.
Note: comparetwofield is a validation rule compiled by us to compare the values of two fields.
4. Define the error message display Statement on the JSP page:
<Spring: bind Path = "tablecrashdo. *"> ----- ①
<C: If test = "$ {not empty status. errormessages}">
<Div class = "error">
<C: foreach Var = "error" items = "$ {status. errormessages}">
<C: Out value = "$ {error}" escapexml = "false"/> <br/>
</C: foreach>
</Div>
</C: If>
</Spring: bind>
① The path must be the same as the bean name that is passed in the JSP. For example, if the data Bean passed into the JSP is tablecrashdo, the path should be written as tablecrashdo .*.
5. Make the client generate JavaScript code:
<V: javascript formname = "tablecrashdo" ----- ①
Staticjavascript = "false" XHTML = "true" CDATA = "false"/>
<SCRIPT type = "text/JavaScript" src = "<C: URL value =" scripts/validator. jsp "/>"> </SCRIPT>
① V: javascript labels are spring labels defined in spring-commons-validator.tld. The formname must be consistent with the form name in validation. xml.
In this way, the verification configuration of the validate can basically be completed.
(Note: commandname in controller; form name in validation. xml; formname of the V: javascript tag in JSP must be consistent .)