Custom validation rules: (Learn)
In the Struts2 custom check rule:
1. Implement a validator interface.
2. Inherit Validatorsupport or Fieldvalidatorsupport in general development
* Validatorsupport: Not for a field check. Check confirmation password (compared to password field)
* Fieldvalidatorsupport: Check for a field.
Steps:
1. Writing a class inheritance Fieldvalidatorsupport
public void Validate (Object obj) throws Validationexception {
Get field Name
String name = GetFieldName ();
Get the value of a field
Object value = GetFieldValue (name, obj);
Judging this value
if (value instanceof Integer) {
Integer i = (integer) value;
if (I < 0) {
Addfielderror (name, obj);
}
}
}
2. Register the Validator: Create a validators.xml under SRC
* The introduction of the Dtd:xwork core package has a XWORK-VALIDATOR-CONFIG-1.0.DTD
* Configure in this XML:
<validators>
<validator name= "AAA" class= "Cn.itcast.struts2.demo5.MyAgeValidator" ></validator>
</validators>
STRUTS2 Custom Validation rules