Struts validation framework
Ube bun (james.hu@chinacodeline.com)
Xi'an Software Co., Ltd.
March 2004
Foreword
I have long heard that the benefits of the Validate framework in Struts1.1, which uses Struts through the project, and simply studies its usage. In this process, I realized its benefits, but it also had some small problems. One is written here with everyone. The environment used by the authors is Struts1.1.
Configure the Struts Validation framework
I believe that the readers who are reading this article are have some experiences for Struts. For Struts to download, how to install the configuration does not have to speak more. Just discuss the installation and configuration of the Struts Validation framework here. Typically, we have two ways to perform this process.
- method one:
Unzip the Struts-Balnk.war to the project's web engineering directory from the WebApps directory comes with Struts. This is the easiest way to create a struts engineering. In this project, it has included all the required development packages of the Struts application and the basic configuration of these packets, and naturally contains the validation framework. It is ideal for the initial stage of the project, what we need is to fill in this blank project.
- Method Two:
For the existing Struts project, how to add the ValiaDation framework support is also very simple.
1. First add the following in Struts-Config.xml
Value = "/ Web-inf / validator-rules.xml, / web-inf / value.xml" /> plug-in> 2. Then create 2 file validator-rules.xml, validation.xml in the web-inflicity. (These two files can also be obtained from Struts-Balnk.war). 3. Copy the class packages required by the Validation framework to web-inf / lib, these classes are: common-validator.jar. This way, we can use the Struts's Validation framework. Use the validation framework It took so many industrials to prepare to use it, in this section, let's take a look at how it is used. Before Struts1.1 appears, verify is performed in the Validate method of the FormBean and the validate method of the action. At the FormBean, we are based primarily of the validity judgment of the interface input, and at the action, mainly the validity of business logic. At the Validation framework proposed in 1.1, individuals think it is more important than the effectiveness of FORMBEAN. Why is STRUTS's creator present a validation framework in Struts1.1? There are too many answers and data online on this area, and it is mainly the reuse, maintenance, and development efficiency of the verification method. Before explanation, you can understand how the Validation framework is used in STRTUTS. problem This is a simple user management program that allows the user information to perform CRUD operations (check and delete). The user information includes a user name, user address, user description, user password. For the above various operations, the constraints are as follows: - Increase and modify: User name, user address, user password, user password confirmation is required, and the value of the password and the confirmation password must be consistent. - Delete and query: The user name is required. Solve and use steps Here, the author lists the Struts1.1 and previous versions of the same part, which is convenient for readers to compare and judgment. 1. Create the basic environment of the Struts project. 2 Support for the Validation framework in 1.1, see the practice of the previous section. 2. Create a corresponding page to determine the page flow. 3. Create a corresponding FORMBEAN: 2 STRUT1.1 version solution: Use DynaActionform for simplicity. DynaValidatorForm is used only because of the Validation framework. Struts-config.xm content: form-bean> Then, modify the value of the validation.xml file under the web-inf directory, where the name of the Form and the names of the domains must be consistent with Struts-Config.xml:
field>
var>
field>
var>
field>
field>
form>
2 1.1 solution before
Horses are real written by ActionForm's subclasses, cover the validate method.
4. Create the corresponding action, and the Validate property of the Action is set to TRUE to enable the Validation framework.
5. Write ApplicationResources.properties to add an error message.
2 Struts1.1
Errors.Required = {0} is required.
.
Errors.maxLength = {0} can not be greater tran {1} character.
Errors.INVALID = {0} is invalid.
ERRORS.BYTE = {0} Must Be a Byte.
ERRORS.SHORT = {0} Must Be a Short.
ERRORS.INTEGER = {0} Must be an integer.
Errors.long = {0} Must Be a long.
Errors.float = {0} Must Be a float.
Errors.double = {0} Must Be a Double.
Errors.date = {0} is not a date.
Errors.Range = {0} is not in the range {1} through {2}.
Errors.creditcard = {0} is an invalid credit card number.
Errors.Email = {0} is an invalid e-mail address.
EditForm.name = name.
EditForm.Password = password.
Editform.Address = address
2 1.1 before the solution, similar to it.
6. Add
From the entire use process, in terms of the verification section, the development efficiency of Struts1.1 has been greatly improved, and the verification method can be multiplexed by Validator-Rules.xml, and the maintenanceability is also great. Get improved. Through Mask, the verification of a single page field is very flexible.
But careful readers may also find that this verification file only indicates that the 2 Password domain is required, but it is not satisfied that they must be equal to this situation. For this, we can use custom validator and will It is added to the Validator-Rules.xml file.
Create custom validator
For the creation of Validator, you can come down to 3 steps:
1. Create a Validator class, Validator must contain a method starting with Valid, and its function signature must be as follows: validxxx (java.lang.object,
Org.apache.commons.valiDator.validatorAction,
Org.apache.commons.validator.field, org.apache.struts.Action.ActionerRRRORS,
Javax.Servlet.http.httpservletRequest, Javax.Servlet.ServletContext)
In this example, the custom validator is as follows (taken to the example provided by Struts, used to provide two domain of equalization check):
Public class customomvalidator {
Public CustomValidator () {
Super ();
}
Public Static Boolean ValidateTwofields (Object Bean,
ValidatorAction Va, Field Field, ActionerRors Errors, httpservletRequest request) {
String value = value = validatorutil.getValueAsstring (bean, field.getproperty ());
String Sproperty2 = Field.getvarvalue ("SECONDPROPERTY");
String value2 = validatorutil.getValueAsstring (bean, sproperty2);
IF (! genericvalidator.isblankornull (value) {
Try {
IF (! value.equals (value2)) {
Errors.Add (Field.getKey (), Resources.getActionError (Request, Va, Field);
Return False;
}
} catch (exception e) {
Errors.Add (Field.getKey (), Resources.getActionError (Request, Va, Field);
Return False;
}
}
Return True;
}
}
2. Add to the Validator-Rules.xml file:
Method = "ValidateTwofields" Methodparams = "java.lang.object, Org.apache.commons.validator.ValidatorAction, Org.apache.commons.validator.field, Org.apache.struts.Action.ActionerRors, javax.servlet.http.httpservletRequest Msg = "errors.twofields" /> Add a corresponding message (ERRORS.TWOFILED) in the corresponding properties file. 3. Use in validation.xml:
var> field> By custom Validator, the extensibility of the Validation framework is greatly improved. Moreover, different verification methods can be reused well