The use of validate in struts

Source: Internet
Author: User

In the Struts-config.xml file, an attribute such as "Validate" is under the action node. In the struts framework, the default property is Validate= "­true". So if you want to validate data by using the overloaded Validate method of form Baens, you don't have to ignore that. Digression: If you set the validate­= "false" in the Struts-config.xml file, the Validate method will not work. This also works with the validate framework to validate data.

Validating data through the Validate framework
1, first in the Struts-config.xml file, append the following code:
<plug-in classname= "Org.apache.struts.validator.ValidatorPlugIn" >
<set-property property= "Pathnames"


Value= "/web-inf/validator-rules.xml,/web-inf/validations.xml"/>
<set-property property= "Stoponfirsterror" value= "false"/>
</plug-in>


2, and then write your own form baens, there are two ways to choose:

Write Formbaen need to inherit validatorform, not 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;
}


。。。。。。


Then configure the Struts-config file
<form-bean name= "UserForm"
Type= "Com.wxhx.presentation.UserForm"/>


② Configuration Struts-config File
<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 Validation.xml Files
<formset>
<form name= "UserForm" >----the UserForm here is form.
Beans's name.
<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>
where key= "Userform.userid" and key= "Userform.password" need to be configured in the resource file
Here's a point to note, for example: <arg key= "Userform.password"
Resource= "false"/>


If more Resource= "false" this sentence, then it will not be taken from the resource file, but directly with the key value to represent.


Above is through validatorform-or
Dynavalidatorform to implement the Validate verification


Attached


Now there's this problem with the Form
Beans can be applied by multiple action, and each action may require different validation fields. The authentication methods that are configured in Validation.xml, such as <form
Name= "UserForm" >) is on this form
Beans carried on. In this case, how to verify it.


Your form.
Beans can inherit Validatoractionform (Dynavalidatoractionform can be configured directly, of course). As shown below:
Public class UserForm extends Validatoractionform {
...



}


Or
<form-bean name= "UserForm"
Type= "Org.apache.struts.validator.DynaValidatorActionForm" >
...
</form-bean>

The Struts-config files are 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 Validation.xml files are configured as follows:
<formset>
<form name= "/technology/createuser
">----here is the path to the action
<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 common rules are defined, and of course you can append new rules yourself.
The word "errors.required" is often seen here, and is also configured through a resource file, with the default resources as follows: msg=


#--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 isn't in the range {1} through {2}.
ERRORS.REQUIRED={0} is required.
Errors.byte={0} must is an byte.
Errors.date={0} is not a date.
Errors.double={0} must be a double.
Errors.float={0} must be a float.
Errors.integer={0} must is an integer.
Errors.long={0} must is an long.
Errors.short={0} must be a short.
Errors.creditcard={0} is isn't a valid credit card number.
ERRORS.EMAIL={0} is an invalid e-mail address.
This is in the case of Struts-blank, when developing a project, note that these default resources

Three, combine the above two kinds of verification
If that's the form
The Validate method is overloaded in beans, and the Validate framework is enabled.
Because you want to enable the Validate framework, Form
Beans needs to inherit validatorform, view the Validatorform source code, and find that the Validate method has been processed (really for the Validate framework &shy; services).
So in form
Validate method is overloaded in the beans, it will overwrite the corresponding function in the original validatorform, so if you want to use it together, you need to be in the form
The beans Validate method calls errors = super.validate (mapping,
request); This code is the work that is required to enable the Validate framework, and then you can judge errors to do the next step.


Of course, you can do the form first.
The Validate method in beans, and then enable the Validate framework, depending on the circumstances.


Iv. enabling client-side validation
Also combined with Validation.xml to verify that I have validation.xml related content in the copy down, easy to see clearly:
<formset>
<form name= "UserForm" >----the UserForm here is form.
Beans's name.
<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>


① in a JSP that requires client authentication Formname= "UserForm"/>
Where FormName is the Validation.xml <form
Name= "UserForm" > the corresponding content


② appends a property to the

Its syntax is: return validate +
Form-bean name + (this) as defined in Struts-config.xml;


③ Append <scriptlanguage= "Javascript1.1"
Src= "staticjavascript.jsp" ></script>


The contents of the staticjavascript.jsp are:


<%@ page language= "java"%>


<%--Set Document type to Javascript (addresses a bugs in Netscape
According to a web resource--%>
<%@ page contenttype= "Application/x-javascript"%>


<%@ taglib uri= "/web-inf/struts-html.tld" prefix= "html"%>




This is only a way, there are other concise (specifically why this, not very clear, hope to get the expert advice)
For example: Append onsubmit= "return validateuserform (this);"

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.