struts2------自定义校验器

        自定义验证器必须实现 Validator 接口,为了简化校验器的开发,框架提供了两个抽象类ValidatorSupport FieldValidatorSupport。其中ValidatorSupport【用于定义非字段优先的校验器】 FieldValidatorSupport【用于字段优先的校验器】 实现了 Validator 接口,同时提供了用于实现数据校验的通用方法。

 

实现用户名称的唯一性校验---自定义校验器

  1. 继承FieldValidatorSupport实现字段有限的校验器

public class UniqueNameValidator extends FieldValidatorSupport {

 

    @Override

    public void validate(Object object) throws ValidationException {

       // 覆盖定义validate方法实现数据校验逻辑

       String name = this.getFieldName();// 获取输入域的名称name

       String value = this.getFieldValue(name, object).toString();// 获取用户输入的数据,已经完成了数据类型转换,可以直接获取到转换后的结果

       // 具体的验证处理逻辑

       if (value != null && value.trim().length() > 0) {

           IUserServ userv = ServiceFactory.getUserService();

           boolean bb = userv.existsName(value);

           if(bb){//验证没有通过,用户名称已经存在,添加配置的默认报错信息

              this.addFieldError(name, this.getDefaultMessage());

           }

       }

    }

}

如果不是很通用的校验,比较建议使用validateXxxx的方法实现,不建议自定义校验器

如果使用了自定义校验方法validateXXX和校验框架两种方法,请注意没有互斥,两个都会被执行

2、注册校验器

将struts2-core.jar中/com/opensymphony/xwork2/validator/validators/default.xml拷贝到src根路径下,并更名为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="creditcard"

       class="com.opensymphony.xwork2.validator.validators.CreditCardValidator" />

    <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" />

    "uniqueName"

       class="com.yao.action.validator.UniqueNameValidator" />

validators>

3、在校验规则说明文件中引用自定义的校验器uniqueName

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="username">

       <field-validator type="requiredstring" short-circuit="true">

           <message>用户名称必须填写!message>

       field-validator>

       <field-validator type="stringlength" short-circuit="true">

           <param name="minLength">6param>

           <param name="maxLength">20param>

           <message>用户名称应该在620个字符之间!message>

       field-validator>

       "uniqueName">

           用户名称已经被占用!

      

    field>

validators>    

你可能感兴趣的:(java)