Note: The struts version is Struts-1.2.7, which is demonstrated on the basis of Struts-blank for future study and reference.
I. Reload the validate method in the corresponding form beans
In the struts-config.xml file, the Action node has an attribute such as "Validate. In the Struts framework, the default attribute is validate = "true ". Therefore, if you want to use the form beans reload validate method to verify the data, you can ignore this. Off question: If you set validate = "false" in the struts-config.xml file, the validate method does not work. This also works for the validate framework to verify data.
Ii. Verify data through the validate framework
1. First in the struts-config.xml file, add the followingCode:
<Plug-in classname = "org. Apache. Struts. validator. validatorplugin">
<Set-Property = "pathnames"
Value = "/WEB-INF/validator-rules.xml,/WEB-INF/validations. xml"/>
<Set-Property = "stoponfirsterror" value = "false"/>
</Plug-in>
2. Write your own form beans in two ways:
① The formbean to be written must inherit validatorform, rather than the actionform In the first method.
Public class userform extends validatorform {
Private string userid = "";
Private string Password = "";
Public void setuserid (string userid ){
This. userid = userid;
}
Public String getuserid (){
Return userid;
}
......
Configure the Struts-config file
<Form-bean name = "userform"
Type = "com. wxhx. Presentation. userform"/>
② Dynamic formbean
<Form-beans>
<Form-bean name = "userform"
Type = "org. Apache. Struts. validator. dynavalidatorform">
<Form-property name = "userid" type = "Java. Lang. String"/>
<Form-property name = "password" type = "Java. Lang. String"/>
</Form-bean>
</Form-beans>
3. Configure the validation. xml file
<FormSet>
<Form name = "userform"> ---- Here, userform is the name of form beans.
<Field
Property = "userid"
Depends = "required">
<Arg key = "userform. userid"/>
</Field>
<Field
Property = "password"
Depends = "required, mask">
<Arg key = "userform. Password"/>
<Var>
<Var-Name> mask </var-Name>
<Var-value> ^ [0-9a-za-z] * $ </var-value>
</Var>
</Field>
</Form>
</FormSet>
Key = "userform. userid" and key = "userform. Password" must be configured in the resource file.
Here, we need to note that: <Arg key = "userform. Password" resource = "false"/>
If resource = "false" is added, it will not be retrieved from the resource file, but will be expressed directly by the key value.
The above verification is implemented through validatorform-or dynavalidatorform
(Appendix)
Currently, form beans can be applied by multiple actions, and each action may require different verification fields. The authentication method configured in validation. XML (for example, <form name = "userform">) is used for this form beans. In this case, how can we verify it?
Your form beans can inherit validatexceptionform (of course, you can directly configure dynavalidatexceptionform ). As follows:
Public class userform extends validatemeditionform {
...
}
Or
<Form-bean name = "userform"
Type = "org. Apache. Struts. validator. dynavalidatexceptionform">
...
</Form-bean>
The Struts-config file is configured as follows:
<Action-mappings>
<Action Path = "/technology/createuser"
Type = "com. wxhx. minihr. createuseraction"
Name = "userform"/>
<Action Path = "/technology/edituser"
Type = "com. wxhx. minihr. edituseraction"
Name = "userform"/>
</Action-mappings>
The configuration of the validation. xml file is as follows:
<FormSet>
<Form name = "/technology/createuser"> ---- Here the path of action is used.
<Field property = "city"
Depends = "required">
<Arg0 key = "prompt. City"/>
</Field>
</Form>
<Form name = "/technology/edituser">
<Field property = "state"
Depends = "required">
<Arg0 key = "prompt. State"/>
</Field>
</Form>
</FormSet>
With regard to validator-rules.xml, some of the common rules are defined, of course, you can also add new rules on your own.
The message MSG = "errors. Required" is often displayed here. It is also configured in the resource file. The default resource is as follows:
# -- Validator --
Errors. Invalid = {0} is invalid.
Errors. maxlength = {0} can not be greater than {1} characters.
Errors. minlength = {0} can not be less than {1} characters.
Errors. range = {0} is not in the range {1} through {2 }.
Errors. Required = {0} is required.
Errors. byte = {0} must be an byte.
Errors. Date = {0} is not a date.
Errors. Double = {0} must be an double.
Errors. Float = {0} must be an float.
Errors. Integer = {0} must be an integer.
Errors. Long = {0} must be an long.
Errors. Short = {0} must be an short.
Errors. creditcard = {0} is not a valid credit card number.
Errors. Email = {0} is an invalid e-mail address.
In the Struts-blank example, pay attention to these default resources during project development.
I will study the rules defined by myself later
3. combine the two verification methods above
If the validate method is reloaded in form beans and the validate framework is enabled, what is the effect?
To enable the validate framework, form beans must inherit validatorform to viewSource codeIt is found that the validate method has been processed (actually serving the validate framework ).
If the validate method is reloaded in form beans, the corresponding functions in the original validatorform will be overwritten. If you want to use the method together, you must call errors = super in form beans's validate method. validate (mapping, request); this code is the work to enable the validate framework, and then judge errors for the next step.
Of course, you can also first use the validate method in form beans and then enable the validate framework, depending on the specific situation.
4. Enable client Verification
It is also verified in combination with validation. xml. I copied the content related to validation. XML to make it easy to see:
<FormSet>
<Form name = "userform"> ---- Here, userform is the name of form beans.
<Field
Property = "userid"
Depends = "required">
<Arg key = "userform. userid"/>
</Field>
<Field
Property = "password"
Depends = "required, mask">
<Arg key = "userform. Password"/>
<Var>
<Var-Name> mask </var-Name>
<Var-value> ^ [0-9a-za-z] * $ </var-value>
</Var>
</Field>
</Form>
</FormSet>
① Add the following sentence to the JSP that requires client verification: <HTML: javascript formname = "userform"/> formname is validation. content corresponding to <form name = "userform"> in XML
② Append an attribute onsubmit = "Return validateuserform (this);" in <HTML: Form );"
Its syntax is: Return validate + form-bean name defined in struts-config.xml + (this );
③ Append <scriptlanguage = "javascript1.1" src = "staticjavascript. jsp"> </SCRIPT>
The content of staticjavascript. jsp is:
<% @ Page Language = "Java" %>
<% -- Set document type to JavaScript (addresses a bug in Netscape according
To a Web Resource -- %>
<% @ Page contenttype = "application/X-JavaScript" %>
<% @ Taglib uri = "/WEB-INF/struts-html.tld" prefix = "html" %>
<HTML: javascript dynamicjavascript = "false" staticjavascript = "true"/>
This is just one way, and there are other conciseness (for specific reasons, I don't quite understand it, I hope I can get some advice from someone else)
For example, append onsubmit = "Return validateuserform (this );"
<HTML: javascript formname = "userform" staticjavascript = "true"/>