Use of struts static dynamic ActionForm-Verification

Source: Internet
Author: User

First: override the validate method of ActionForm. In actionForm, form verification is required rather than business verification. For example, the user name is not empty, the password is a number, and the date is in the time format. In the validate method, the ActionErrors information is returned, and the error message is printed on the error page. [Html] public ActionErrors validate (ActionMapping mapping, HttpServletRequest request) {ActionErrors errors = new ActionErrors (); if (username = null | "". equals (username) {errors. add (username, new ActionMessage ("error. username ", username);} if (password = null | "". equals (password) {errors. add ("password", new ActionMessage ("error. password ", password);} if (! (Birthday instanceof Date) {errors. add ("birthday", new ActionMessage ("error. birthday ", birthday);} return errors;} public ActionErrors validate (ActionMapping mapping, HttpServletRequest request) {ActionErrors errors = new ActionErrors (); if (username = null | "". equals (username) {errors. add (username, new ActionMessage ("error. username ", username);} if (password = null | "". equals (password) {errors. add ("Password", new ActionMessage ("error. password", password);} if (! (Birthday instanceof Date) {errors. add ("birthday", new ActionMessage ("error. birthday ", birthday);} return errors;} code explanation: to use this ActionForm repeat verification method, set the validate attribute of this action to true in the configuration file, you must also set the input attribute of the error page returned when an error occurs. The configuration of action in the struts-config.xml is as follows: [html] <action-mappings> <action path = "/login" type = "com. login. loginAction "name =" loginForm "scope =" request "input ="/login/error. jsp "validate =" true "> <forward name =" success "path ="/login/success. jsp "> </forward> <forward name =" false "path ="/login/false. jsp "> </forward> </action-mappings> <action path ="/login "type =" com. login. loginAction "nam E = "loginForm" scope = "request" input = "/login/error. jsp "validate =" true "> <forward name =" success "path ="/login/success. jsp "> </forward> <forward name =" false "path ="/login/false. jsp "> </forward> </action-mappings> to display errors, we use the resource file method. In the validate method, "error. birthday" in new ActionMessage ("error. birthday", birthday) is the key value of the resource file. Load the resource file in the configuration file: [html] <! -- Load the resource file --> <message-resources parameter = "MessageResources"/> <! -- Load the resource file --> <message-resources parameter = "MessageResources"/> note that the resource file is placed under src. If it is stored in the WebContent directory or web-inf, no error message is displayed. Even if no error is reported. In addition, the above date is registered using Register, which has been introduced in the previous blog and will not be described too much here. The demo of this action has been uploaded. Click here to download. Type 2: Use the validator framework. Among them, validate verification, mainly by the commons-validator.jar package. So how to use the validator framework? 1. actionForm uses the validator framework. First, write the actionForm. Note that the actionForm does not inherit the ActionForm, but the ValidatorForm. In actionform, only the getset method is followed: Write the configuration file struts-config.xml, load the authentication plug-in and use the resource file. [Html] <! -- Load the resource file --> <message-resources parameter = "MessageResources"/> <! -- Verify the plug-in --> <plug-in className = "org. apache. struts. validator. validatorPlugIn "> <set-property =" pathnames "value ="/WEB-INF/validator-rules.xml,/WEB-INF/validation. xml "/> </plug-in> <! -- Load the resource file --> <message-resources parameter = "MessageResources"/> <! -- Verify the plug-in --> <plug-in className = "org. apache. struts. validator. validatorPlugIn "> <set-property =" pathnames "value ="/WEB-INF/validator-rules.xml,/WEB-INF/validation. xml "/> </plug-in> where the validator-rules.xml is public. This validation. xml is written by a programmer. Then: Write the validation. xml verification information. [Html] <? 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> <! -- The name of form must be the same as that in struts-config --> <form name = "loginForm"> <field property = "username" depends = "required"> <! -- The key value must be the same as the name in the resource file --> <arg key = "error. username "/> </field> <field property =" password "depends =" required "> <arg key =" error. password "/> </field> <field property =" birthday "depends =" date "> <arg key =" error. birthday "/> </field> </form> </formset> </form-validation> <? 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> <! -- The name of form must be the same as that in struts-config --> <form name = "loginForm"> <field property = "username" depends = "required"> <! -- The key value must be the same as the name in the resource file --> <arg key = "error. username "/> </field> <field property =" password "depends =" required "> <arg key =" error. password "/> </field> <field property =" birthday "depends =" date "> <arg key =" error. birthday "/> </field> </form> </formset> </form-validation> formset can contain multiple forms, and field labels are used for attributes to be verified in form. Where depends on rules in the Information validator-rules.xml of the validation rule. For example: required, required, date, date format, long, byte, mask regular, and so on. The key value must be the same as the key of the resource file. For example, the first method. 2. Dynamic actionform uses the validator framework. The dynamic actionform has been introduced in the previous blog. On the basis of 1, only change the dynamic actionform in the struts-config.xml configuration file [html] <! -- Dynamic verification form --> <form-beans> <form-bean name = "loginForm" type = "org. apache. struts. validator. dynaValidatorForm "> <form-property name =" username "type =" java. lang. string "> </form-property> <form-property name =" password "type =" java. lang. string "> </form-property> <form-property name =" birthday "type =" java. util. date "> </form-property> </form-bean> </form-beans> <! -- Dynamic verification form --> <form-beans> <form-bean name = "loginForm" type = "org. apache. struts. validator. dynaValidatorForm "> <form-property name =" username "type =" java. lang. string "> </form-property> <form-property name =" password "type =" java. lang. string "> </form-property> <form-property name =" birthday "type =" java. util. date "> </form-property> </form-bean> </form-beans>. However, it should be noted that the type of the dynamic actionform using the validator framework is DynaValidatorForm rather than DynaActionForm. 3. Use js prompts on the client. On the basis of 1, only one login. jsp is changed. Add onsumit and add html: javascript tags [html]

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.