Validation Framework Experience

xiaoxiao2021-03-06  62

1 Validation Framework

In Struts Framework, the ActionForm object is used to package the HTML table data (including request) and return to the data that is returned to the user. They must be complete JavaBean, inherit. The Actionform class inside. At the same time, the user can selectively cover two default method validate () and reset ().

Disadvantages of this: 1. Calibration code redundancy and cannot be reused.

2. It is difficult to maintain it. Validation Framework separates the calibration part in the ActionForm and implements the configuration file. The following will be written under the experience in using the Validation. Due to time, it is basically no modification for the original text, and it may be a bit chaos. Please forgive me. 1.1 Composition and Configuration 1.1.1 Composition Package: Commons-Validator.jar, Jakarta-oro.jar,

Profile: Validator-Rules.xml, Validation.xml

1.1.2 Configuration requirements: 1, there is validation.xml, validator-rules.xml file in web-inf / directory

2, there is the following in web-inf / struts-config.xml

1.2 Validation-Rules.xml Validation-Rules.xml defines a set of universal verification rules that will be referenced in Validaion.xml. You can also customize some rules to this file, or you can separate a file separately. For example:

Name = "minlength"

ClassName = "org.apache.struts.util.strutsvalidator"

Method = "ValidateMinLength"

Methodparams = "java.lang.object,

Org.apache.commons.valiDator.validatorAction,

Org.apache.commons.validator.field,

Org.apache.struts.Action.Actionerro,

Javax.Servlet.http.httpservletRequest "

Depends = "Required, INTEGER"

Msg = "ErrorS.minLength">

Attributes

description

Name

Specify a rule name to be referenced by other rules

Classname

Class containing check rules

Method

This verification rule actually corresponds to the class

Methodparams

Parameters of the Method method

Depends

Reference other rules (called before the current rules)

MSG

The keywords in the resource file, when the verification is not passed, the information in this keyword will be added to the ERRORS collection.

JSFunctionName

Specify a JavaScript function name

"Rg.apache.struts.util.strutsvalidator" defines the following methods:

ValidateByte

Validatecreditcard

Validatedate

Validatedouble

ValidateEmail

Validatefloat

ValidateInteger

Validatelong

Validatemask

· Validateminlength

ValidateMaxLength

ValidateRange

ValidateRequired

ValidateShort

1.3 Validation.xml Validation.xml Association ActionForm and Validaion-Rules.xml define the verification rules for each attribute in the ActionForm. E.g:

phone

^ / (? (/ d {3}) /)? [- |]? (/ d {3}) [- |]? (/ d {4}) $

Mask

$ {phone}

minlength

5

This profile specifies the "Phone" attribute in "Required, Mask, MINLENGTH" to verify "CheckoutForm": "Phone" is not empty, must be greater than or equal to 5 characters, the character must meet $ {Phone } The rule defined.

: Specifies the actionform to be verified

Attributes

description

Name

Specify the name of the ActionForm to be verified

: Specify which property to verify ActionForm

Attributes

description

Property

Specify the name of the attribute in the actionform to be verified

Depends

The verification rules used are defined in Validation-Rules

page

• This field will be processed when the value of this attribute is less than or equal to the PAGE value of this FormaBean.

IndexedListProperty

. The method name triant return an array or a collection use to retrieve the list and the loop through the list, performing the validations for this field.

: When the verification fails to the information in Action Errors, used to replace the default error information in Validator-Rules

Attributes

description

Name

Use the rule name of the verification

Key

Key or custom values ​​in the resource file

Resource

When set to false, the key value can be customizable.

: 4 (arg0..arr3) can be defined, and the role is to provide the value of the parameter to the resource file. Attributes with

To use server-side authentication, the Action Form implementation class should extend ValidatorForm instead of ActionForm. When submitting a form, the validate () method in the ValidatorForm will be verified according to the value of validation-rules.xml and the validation.xml file.

You need to add rows in the beginning of the JSP file (ie ) to make the error displayed.

This is different from the use of client verification.

1.4 Use the client's check (1) Put at the end of the JSP file, ie before. It works in the JavaScript function page of Validation-Rules.XML with "CheckoutForm".

(2) OnSubmit properties in . For example: >

Description: The validateCheckOutform () method is automatically generated in step (1). The validatexxxx () method will be generated according to the property value of the FormName (for example: xxx).

(3) Action Form should remove the Validator method.

2 extended object selection

When selecting an extended object for Form, pay attention to: The Struts framework provides two options. The first choice is to use the ValidatorForm or DynavalidatorForm; the second option is to use the ValidatorActionform or DynavalidatorActionForm.

When using the first method, the Name property of the FORM element in Validator.xml must match the Name property of the Action element in Struts-Config.xml.

When using the second method, it is matched to the Path property of the Action element in Struts-Config.xml.

转载请注明原文地址:https://www.9cbs.com/read-84950.html

New Post(0)