Struts validation framework

xiaoxiao2021-03-06  44

I have long heard it.

Struts1.1

in

Validate

The benefits of the framework, this time is adopted by the project.

Struts

Simple research on its usage. In this process, I realized its benefits, but it also had some small problems. One is written here with everyone. The author used by the author 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" />

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:

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:

Mask

^ [0-9a-za-z] * $

Mask ^ [0-9a-za-z] * $

2 1.1 previous solution

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.minLength = {0} CAN NOT BE LES THAN {1} Characters.

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 The previous solution, similar to it.

6. Add in the corresponding JSP.

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:

SecondProperty

PWD2

By custom Validator, the extensibility of the Validation framework is greatly improved. Moreover, different verification methods can be reused well

JavaScript support

In web applications, there are many opportunities to use JavaScript. Although many books mentioning that customers may have the implementation of JS from the browser, don't use it completely, it seems that it doesn't seem to be. For example, some complex UIs must be implemented by JS (such as pull-down menus, etc.). So what is the JS authentication of the Validation framework not supported?

The answer is: Of course. The specific approach is:

- Add οnsubmit = "Return ValidateEditform (this) in the page HTML: Form Tag; (specific syntax: Validate defined in the validation.xml file);

>>>>>>>>

- Add:

So 2 steps. Although the Validation framework is very easy to use, it still needs attention.

Use attention

1. After using the Validation framework, Form must derive from the ValidatorForm, and must first call the validate method of the base class in your validate method. For methods that start with Dyna to create a FORMBEAN reader, you must also change the FORM with Validator with DYNA.

2. Note DynaValidatorForm (ValidatorForm) and DynavalidatorActionform (ValidatorActionform). At the beginning, I didn't understand the difference between these two, and later I obtained the difference from the online article. The main perspective of the former is FORMBEAN, while the latter's perspective is Action.

When FormBean is used by different Action, for different Actions, it is very small. At this point, if you still use the FORMBEAN as the main body, it will cause abnormal use of other Actions. Therefore, the Struts proposed DynavalidatorActionform (ValidatorActionform). At this time, the Name property of the FORM tag in Validation.xml is changed to the path properties of Action, and because there is Attribute and Name attribute in the action, the Validation Framework can get the corresponding FORMBEAN according to this action. Example:

Depends = "Required">

Depends = "Required">

3. Team with Dispatchaction. Struts1.1's Dispatchaction makes the relationship of the relevant Action, greatly reduces the number of Actions in the application, but with the inconvenience of using the Validation framework, it cannot be said to be a regret. The reader may think that this situation can be used to solve the solution of Article 2, that is, use DyanValidatorActionForm, and then uses different ActionD's path in the validation.xml file, ie in validation.xml: , . However, this Identification framework does not support this recognition in the current version. A method of bypassing this is that the PATH defined in the struts-config.xml file in the Struts-Config.xml file is set to false in the struts-config.xml file. which is:

Struts-config.xml:

Parameter = "Method" scope = "request" type = "foxgem.struts.userdispatchaction"

Validate = "True">

Parameter = "Method" scope = "request" type = "foxgem.struts.userdispatchaction"

Validate = "false">

Then use 2 methods in the validation.xml file.

Conclude

In general, the Validation framework greatly enhances the development efficiency of page validation, more attractive, these verification methods can be reused by custom Validator. Allow these verification code more concentrated, maintaining maintainability. Of course, as the project is carried out, Validation.xml and Validator-Rules.xml will grow, and this part of the maintenance work is aggravated.

At the same time, since it is unable to collaborate with Dispatchaction, a large number of items that use Dispatchaction cannot be used very well. It is recommended to use Dispatchaction and page Verification Very complex and multi-change projects to be verified by the original way and do not use the Validation framework.

For additional details of the Validation framework, see the Struts documentation, and details are not described here.

Reference

n http://raibledesigns.com/wiki/wiki.jsp?page= Subsequent solutions for Dispatchaction and the Validation framework come from this.

n http://otn.racle.com/oramag/oracle/04-jan/o14dev_struts.html Check your form with validator

n http://www-900.ibm.com/developerWorks/cn/java/l-struts1-1/ in-depth Struts 1.1

n http://javaboutique.Internet.com/tutorials/struts11val/tepping through the struts 1.1 Validator

转载请注明原文地址:https://www.9cbs.com/read-78018.html

New Post(0)