Sie sind auf Seite 1von 4

The data validation rules must also be separated from the source code so you

can modify the validation rules without affecting applications that are in
production mode.
The data validation routines must be independent of the surrounding process.
This means that the same validation routines should work identically in all
applications, regardless of the way input is supplied to the routines.

The validation process using the Validator component

As shown in the figure, these are the steps of the validation process:
1. Create or modify a wrapper class around validation rules.
2. Define the validation rules for a targeted data set. (In xml)
3. accept user input data as a Java Bean.
4. Create an instance of Validator.
5. Pass in the rules and user data to this instance.
6. Call the validate method of the Validator instance, and accept results in
the ValidatorResults class instance.
7. Process the results.

Listing 6.1 First draft of the wrapper class


package com.manning.commons.chapter06;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.GenericValidator;
import org.apache.commons.validator.util.ValidatorUtils;
public class ValidatorWrapper {
public static boolean doRequired(Object bean, Field field) {
String value =
ValidatorUtils.getValueAsString(bean, field.getProperty());
return !GenericValidator.isBlankOrNull(value);
}
}

Listing 6.2 Creating a targeted set of validation rules in the file


validatorsimple.xml
<!DOCTYPE form­validation PUBLIC
"­//Apache Software Foundation//DTD Commons Validator Rules
Configuration 1.2.0//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_2_0.dtd">
<form­validation>
<global>
<!­­ Define targeted validator  ­­>
<validator name="required"
classname="com.manning.commons.chapter06.ValidatorWrapper"
method="doRequired"
methodParams="java.lang.Object, 
org.apache.commons.validator.Field"
msg=""/>
</global>
<formset>
<form name="simpleform">
<field property="firstName" depends="required"></field>
<field property="lastName" depends="required"></field>
</form>
</formset>
</form­validation>

Listing 6.3 A simple JavaBean


package com.manning.commons.chapter06;
public class SimpleJavaBeanV1 {
private String firstName;
private String lastName;
public String getFirstName() { return this.firstName ; }
public void setFirstName(String firstName) { this.firstName = 
firstName; }
public String getLastName() { return this.lastName; }
public void setLastName(String lastName) { this.lastName = 
lastName; }
}

Listing 6.4 Performing validation for a simple JavaBean instance


package com.manning.commons.chapter06;
import org.apache.commons.validator.Validator;
import org.apache.commons.validator.ValidatorResult;
import org.apache.commons.validator.ValidatorResults;
import org.apache.commons.validator.ValidatorResources;
import java.io.FileInputStream;
public class ValidatorSimpleV1 {
public static void main(String args[]) throws Exception {
ValidatorResources resources =
new ValidatorResources(
new FileInputStream("validatorsimple.xml"));
Object userData = simulateUserData();
Validator validator = new Validator(resources, "simpleform");
validator.setParameter(Validator.BEAN_PARAM, userData);
ValidatorResults results = validator.validate();
ValidatorResult result =
results.getValidatorResult("firstName");
ValidatorResult result1 =
results.getValidatorResult("lastName");
System.err.println("First Name: " +
result.isValid("required"));
System.err.println("Last Name: " +
result1.isValid("required"));
}
private static Object simulateUserData() {
SimpleJavaBeanV1 simpleBean = new SimpleJavaBeanV1();
simpleBean.setLastName("Goyal");
return simpleBean;
}
}

• The ValidatorResources  class loads the targeted rules that were


created in the XML file from listing 6.2. This class loads the rules and
uses the Digester component to parse them.
• The Validator instance is now created. It accepts two arguments. The
first, as expected, is the ValidatorResources  instance. This instance
initializes Validator  to process the targeted rules defined in the XML
file. The second argument, in this case simpleform, is the name of the
<form> element in the XML file to be processed. Recall that the <form> 
element can occur multiple times within the XML file. This means that
the same XML file can be used to define the processing of several
forms. By specifying which <form>  element to process by name, the
Validator instance is able to pick the correct one in this situation.
• The Validator instance is now told about the user data that is to be
validated. This user data was created (simulated). Note the way in
which Validator is told about the user data: It’s specially set using the
setParameter method, and the constant Validator.BEAN_PARAM
identifies the JavaBean on which validations are to be performed.
Needless to say, if you’re doing multiple validations, and each piece of
user data is represented by a different JavaBean instance, you’ll need
to reset this parameter.
• Finally, we can perform the actual validation. The validate method
returns the result of the validation in the ValidatorResults class. This
class contains a HashMap of individual ValidatorResult objects,
corresponding to each field that was processed where the field name is
used as the key for each result. Thus, to get the validation result for the
lastName property, we use getValidationResult("lastName").

Das könnte Ihnen auch gefallen