假设jsp里有个字段username[],怎么用验证框架对username[i]进行验证呢?最好有例子。
------解决方案--------------------
例如:对一个Action中的一个数组字段userNames校验:
public class TestArrayValidateAction {
private String[] userNames;
public String[] getUserNames() {
return userNames;
}
public void setUserNames(String[] userNames) {
this.userNames = userNames;
}
public String execute() {
return "success";
}
}
分三步:
第一步:
定义自定义校验器:
java代码:
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;
}
}
第二步:注册自定义校验器,在classpath下定义validators.xml:
<?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"/>
<validator name="url" class="com.opensymphony.xwork2.validator.validators.URLValidator"/>
<validator name="visitor" class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/>
<validator name="conversion" class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/>
<validator name="stringlength" class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/>
<validator name="regex" class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/>
<validator name="conditionalvisitor" class="com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/>
<validator name="MyValidator" class="zzg.study.struts2.validators.MyValidator"/>
</validators>
这一步要注意,要把struts中default-package的校验器都声明一遍,否则原来的校验器不起作用了。
第三步:
在actionName的包中定义校验配置文件TestArrayValidateAction-validation.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<field name="userNames">
<field-validator type="MyValidator">
<message>The index:${index} has error!</message>
</field-validator>
</field>
</validators>
写一个jsp文件测试:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>