Each application is responsible for ensuring that the data they are inserted into the latter library is legally effective. After all, if the data relying on these applications is destroyed, it will be disastrous. What can I take anything? Come to make yourself working properly? For example, an application using a regular relational database, each field in the database has its own rules and constraints to ensure that the data stored in it is properly correct. Any application of the Taiwan-funded material database data is responsible for protecting the integrity of the data they submitted. Any operation attempts to insert or update data that does not meet the standard may be discovered and rejected. This detection may be spread throughout the entire application, and there may be some verification in the performance layer, and the business logic layer, the business logic object is generally verified, and the data is also checked in the post-Taiwan investment. Unfortunately, since this verification is ubiquitous in the application, it has caused code redundancy of applications to a certain extent. This is not what the app is hoped because this repetitive labor is repeated in a plurality of times, making the application deployment and maintenance take more time. If these validation rules can be reused throughout the app, they will make the application more flexible. In other words, the deployment is faster, the customization is easier, and the program is more flexible. Jakarta Commons Project Validator Framework Introduction Validator is an open source project created by David Winterfeldt, which is also a sub-project for Jakarta Commons. The Commons project is primarily providing some reusable components like Validator. Other well-known Commons components are as BeanUtils, Digester, Logging frameworks, and more. Validator version 1.0 was posted in early November 2002. Use the benefits of Validator. Use the Validator framework to define a lot of advantages than the average verification rules in the application's code, such as: You can define a verification rule for the application;. Verification rules and applications are loose coupling; The server-side and client authentication rules can be defined at the same point; configure new verification rules or modify existing validation rules become simpler; support international; support regular expressions; can be used for web applications Standard Java application; use declarative method to implement rather than programming; except, Validator's largest feature is its own support of pluggability. Behind the article you will see the verification rules built in the Validator framework to better complete your work, and more importantly, the Validator Framework allows you to customize the authentication program and insert them into the frame. The relationship between Struts and Validator should point out that the Validator framework itself is built due to the Struts framework. Validator's creator David Winterfeldt discovered in the process of using Struts, which requires repeated use of the same verification rule in many ACTIONForm classes, which causes a lot of code redundancy. So he decided to create a Validator framework to eliminate this redundancy, so that Validator was born. Although the Validator architecture was originally born for the Struts architecture, it was still designed and configured to be used separately from the Struts architecture. This feature allows you to use this frame in any application, don't take it to the Struts architecture. It doesn't affect your application role because you don't use the Struts framework. In fact, this is why Validator is part of the Jakarta Commons project and is not part of the Struts project. Now let's integrate this framework to web applications like the Struts architecture.
In the final midth of the article, we will show how to apply it to other types of applications, such as EJB-based applications. Validator Components Overview The Validator architecture has the following components: validators; profile; resource binding The framework calls this Validaotor class based on the method defined in the configuration file. In general, each Validator class provides a separate verification rule, and then these rules can be combined into a more complex rule set. Note: Sometimes it is convenient, a Validator class can also define multiple authentication rules, and each rule is a static method and does not contain any client status information. The framework provides 14 default authentication rules, sometimes these rules are also known as the "Basic Rules" of the Validator framework, these basic rules, such as Table 1: Name Description Byte, Short, Integer, long, float, double verification value can Converted into a corresponding basic data type CREDITCARD check whether the input domain is a legitimate credit card number Date check whether the input domain is a legal date EMAIL verification input is a legal Email address MASK verification input domain can successfully match a regular expression Maxlength Whether the length of the test value is less than or not the length of the given maximum length minlength test value is greater than or not, whether the range of the given minimum length RANGE verification value is not empty, or if the REQUIRED test input domain is not empty, or Whether the length of the space does not contain a space value is greater than zero
Just as seen in Table 1, the Validator framework provides most of the validation rules that the web application needs. You can use these existing verification rules to create your own verification profile. Despite this, as we mentioned earlier, and later, you can add more Validator according to your needs. Now let's discuss how to configure these basic validator in a Struts architecture-based application. The reason for the Validator framework is flexible in that all verification rules and their specific details are implemented by configuring declarations in external files. Your app doesn't have to know these specific verification rules. This feature allows you to expand and modify the rule set, you don't have to move your application's source code. This is very important for you to perform each personalized installation or when the demand changes. If you use the struts1.1 Validator framework, you will use the two configuration files, a validator- rules.xml, another called validation.xml; actually you can also give them free to name, and you can merge them A XML file. However, you are still best to separate them because they have their own uses. Note: If you download Validator from the Jakarta website, it does not contain both files. These two files can only be found in the Struts of the Validator Framework. Validator-rules.xml file validator-rules.xml file Define the validator that the application can use. Validator-rules.xml acts as a template's role, define the validator that all applications may be used. Note: This XML file and another XML file we have to discuss below should be placed in the class loader. When we use the Validator framework in a web application, the correct location should be under Web-INF. Validator-rules.xml file service from validator- rules_1_1.dtd, validator- rules_1_1.dtd can be downloaded in Jakarta.apache.org/struts/dtds/Validator- rules_1_1.dtd. We don't want to spend too much time to study the specific details of this document, we only make some basic introductions here. The most important element in the Validator-Rules.xml file is included in the element, for example, an example one:
Example 1: A simple validator-rules.xml file
Name = "required" ClassName = "org.apache.struts.util.strutsvalidator" Method = "ValidateRequired" Methodparams = "java.lang.object, Org.apache.commons.valiDator.validatorAction, Org.apache.commons.validator.field, Org.apache.struts.Action.Actionerro, Javax.Servlet.http.httpservletRequest " Msg = "ErrorS.Required" /> ClassName = "org.apache.struts.util.strutsvalidator" Method = "ValidateMinLength" Methodparams = "java.lang.object, Org.apache.commons.valiDator.validatorAction, Org.apache.commons.validator.field, Org.apache.struts.Action.Actionerro, Javax.Servlet.http.httpservletRequest " Depends = "reguired" Msg = "errors.minLength" /> global> form-validation> Every validator used by the application element. In the example one, I showed two Validator, one is requested Validator and the other is the minimum length Validator. Elements support many properties. These attributes are necessary to inform the framework which correct class and method should call this validator. For example, the requested Validator element in Example One indicates that this Validator will call the validateRequest () method of the org.apache.struts.util.StrutsValidator class. Validator may also rely on another Validator, such as the minimum length of the unique length is an example, which contains a Depends property that uses it to indicate that this Validator will depend on request validator. The MSG property specifies a resource binding with a key value, and the framework will use it to generate the correct error message. The use of resource bindings is beneficial to the localization of error messages. Element also supports Child elements allow you to specify a JavaScript function running on a client. This allows the server and client verification to be specified at the same point, which makes the application's maintenance simple. Validation.xml file The second profile of the Validator Frame is this file called Validation.xml. In fact, you can name it as any name you like, or put it in the validator-rules.xml file. Validation.xml is used to map all validator you defined in Validator-Rules.xml and components in your application. Since we discussed here is the use of the Validator framework in Struts, then validation.xml here is a mapping of these Validator and Struts. The ActionForm class is actually a class similar to JavaBean, which is used to capture user input and help to transfer these inputs to the next application components. The ActionForm also provides a convenient place to verify these inputs before user input is transmitted to the business logic layer. Example 2 is a simple validation.xml: Example 2: A simple validation.xml file Property = "firstname" Depends = "Required"> field> Property = "Lastname" Depends = "Required"> field> form> formset> form-validation> Example I show a name attribute called CheckoutForm. CheckoutForm is an Actionform Bean defined in the Struts configuration file. Therefore, the XML file of Example 2 is to establish this ActionForm Bean and request Validator to establish a map, the bean's firstname and the lastname property correspond to the corresponding firstname and the lastname field element in the XML file. In fact, it has many other functions, such as defining constants and global variables in Validation.xml, used to use throughout the file, when you want to make it easier and repeatedly. For more detailed explanations for element and properties of Validation.xml, you can download jakarta.apache.org/struts/dtds/validation_1_1.dtd to see. Author: Chuck Cavaness Translator: javaduke Programming