Detailed description of struts2 validate Mechanism

Source: Internet
Author: User
Verification is a function required by all web frameworks. Next let's analyze the verification problem:

Classification of verification problems:

    • Transformation validation: because all the data submitted on the page is text, and the Web Framework processes objects, the transformation is required.

Verification is inevitable during the transformation process.

 

    • User-Defined data verification: after the transformation, data users often have many custom verification requirements, such as the age must be a natural number, therefore, you must allow users to use descriptive configuration to describe each field in the form. However, this verification generally does not involve services in the model.

 

    • Verification involving the model: Some verifications must be performed only when the backend service is accessed. For example, the username and password verification during logon must be performed by accessing the services in the model.

 

The solution to the above three verification problems and the order of verification constitute the basic idea of the strurts2 verification framework. among them, parametersinterceptor and conversionerrorinterceptor solve transformation verification, and validationinterceptor solves user-defined data verification and involves model verification.

 

1. parametersinterceptor

Convert the form value submitted in parameters to valuestack. When type conversion error occurs during the transformation, set key = com. opensymphony. xwork2.actioncontext. in the map corresponding to conversionerrors, save the attribute name and value pairs of the type convertion failure.

Class: COM. opensymphony. xwork2.interceptor. parametersinterceptor

Method: protected void setparameters (Object action, valuestack stack, final map <string, Object> parameters)

Code:

Stack. getcontext (). Put (actioncontext. conversion_errors, newstack. getcontext (). Get (actioncontext. conversion_errors ));

 

2. conversionerrorinterceptor

Generate an error message based on the attribute name for type conversion failure and Its Value Combined with the error information template. Add the error information to fielderror of the corresponding field in validationaware.

Class: COM. opensymphony. xwork2.interceptor. conversionerrorinterceptor

Method: Public String intercept (actioninvocation Invocation)

A) string message = xworkconverter. getconversionerrormessage (propertyname, stack );

Class: COM. opensymphony. xwork2.conversion. impl. xworkconverter

Method: public static string getconversionerrormessage (string propertyname, valuestack stack)

I. go to prop key = xwork. default. invalid. fieldvalue value, which is found in org/Apache/struts2/Struts-messages and COM/opensymphony/xwork2/xwork-messages by default.

Ii. Use prop key = invalid. fieldvalue. + fieldname to find the corresponding information.

Iii. If II exists, an error message is generated based on II. Otherwise, I is used.

B) validationaware Va = (validationaware) action;

Va. addfielderror (propertyname, message );

Put the generated error information into fielderror

 

3. validationinterceptor

Class: COM. opensymphony. xwork2.interceptor. validationinterceptor

Method: protected void dobeforeinvocation (actioninvocation Invocation)

A) actionvalidatormanager. Validate (action, context );

Call all validator verification data and store fielderror and actionerror in validationaware.

B) prefixmethodinvocationutil.Invokeprefixmethod(

Invocation,

NewString [] {Validate_prefix,Alt_validate_prefix});

Check whether there are validate, validatedo + method methods. If there are methods in order, return

C) Call validateable. Validate ();

 

4. Display Error information:

Use the <s: fielderror> label. The attribute name can be specified in <s: param> price </S: param>.

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.