日期:2014-05-17 浏览次数:20846 次
public class TestArrayValidateAction {
private String[] userNames;
public String[] getUserNames() {
return userNames;
}
public void setUserNames(String[] userNames) {
this.userNames = userNames;
}
public String execute() {
return "success";
}
}
import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;
public class MyValidator extends FieldValidatorSupport{
public void validate(Object object) throws ValidationException {
String fieldName = getFieldName();
String[] userNames = (String[]) getFieldValue(fieldName, object);
validateArray(object, fieldName, userNames);
}
private void validateArray(Object object, String fieldName, String[] userNames) {
//下面可以换成自己需要的代码。
if(userNames == null)
addFieldError(fieldName, object);
for(int i=0;i<userNames.length;i++) {
System.out.println("正在对userNames第"+i+"个进行校验");
}
}
public int getIndex(){
return 0;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator Definition 1.0//EN"
"http://struts.apache.org/dtds/xwork-validator-definition-1.0.dtd">
<validators>
<validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/>
<validator name="requiredstring" class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/>
<validator name="int" class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/>
<validator name="long" class="com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/>
<validator name="short" class="com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/>
<validator name="double" class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/>
<validator name="date" class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/>
<validator name="expression" class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/>
<validator name="fieldexpression" class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/>
<validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/>
&nbs