Using Validator includes the following three steps: 1, enable the Validator plugin; 2, generate the Form Bean class; 3. Modify the configuration file. Here's how to configure and use Validator.
First, enable the Validator plugin
Although Validator is included in the Struts kit, the validator is not enabled by default. To enable Validator, you must add the following code to the Struts-Config.xml file:
Value = "/ Web-inf / validator-rules.xml, /Web-inf/Validation.xml "/> plug-in> Where the specified two configuration files should be the path required by the DTD file corresponding to the Stuts-Config.xml configuration file, and the above code should be placed in the appropriate location according to the format required by the DTD file corresponding to the Stuts-Config.xml configuration file. Second, generate the Form Bean class In order to use Validator, the generated Form Bean class must inherit the subclass of ActionForm ValidatorForm. In the Form Bean class, you want to include member variables corresponding to each field in the Form form, and then disclose the GET and SET methods of each member variable. The sample code is as follows: Package com.my; Import org.apache.struts.validator.validatorform; Public class myform extends validatorform { Public string getage () { Return Age; } Public void setage (String age) { THIS.AGE = AGE; } Public string getname () { Return Name; } Public void setname (String name) { THIS.NAME = Name; } PRIVATE STRING NAME; Private string agec; } Third, modify the configuration file Specify the Form Bean in the struts-config.xml file. The sample code is as follows: TYPE = "com.my.myform" /> form-beans> There is a validatotr-rules.xml file in the STRUTS development package, copy it to the web-inf directory of the application module (of course, you can also create and write this file yourself). In the same directory, create a new Validation.xml file and write verification content. In order to facilitate writing, introduce the corresponding DTD file (recommended validator_1_1_3.dtd). The sample code is as follows: Depends = "Required, MINLENGTH, MAXLENGTH"> Key = "$ {var: minLENGTH}" Resource = "false" /> Key = "$ {var: maxlength}" Resource = "fasle" />
var>
var> field> Depends = "Required, INTEGER, INTRANGE"> Key = "$ {var: min}" Resource = "false" /> Key = "$ {var: max}" Resource = "false" />
var>
var> field> form> formset> form-validation> Since Validator wants to read Error Messages from the resource file, you have to configure the resource file. Create a resource file in the com.my package, named Application.properties (extension must be Propertyies), the content is as follows: # Struts validator error messages Errors.Required = {0} is required. Errors.minLength = {0} CAN NOT BE LES THAN {1} character. 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. In order to enable the container to load the resource file, set the setting in the struts-config.xml file, add the following code in the appropriate position: 4. To this, the Validator configuration work has been completed. To test Validator, you also need to write a form (JSP) page, create an action, and configure actionmapping, see the relevant chapter.