【Spring RCP】 RULES规则

 

Rich Client 约束规则

 

 

 

 


 

1.Constraint 定义了一个约束接口,接口中只有1个方法

public boolean test(Object argument); //这个方法指对约束的检测

 

2.AbstractPropertyConstraint为Constraint 接口的抽象实现类,在原有基础上封装了“propertyName”字段,用于记录一个约束针对的标识

 

3.PropertyValueConstraint 是对 AbstractPropertyConstraint进一步的封装,实现针对某个额字符串属性,的检测约束

 

4.Rule 约束规则对象

public class Rules extends ConstraintsAccessor implements Constraint, PropertyConstraintProvider, Validator,InitializingBea

 

  1) Rule 对象定义了一个Hash属性保存所有需要检测对约束规则

    private Map propertiesConstraints = new HashMap()

  2) 可以通过add方法将约束规则存入Map中,保存时候,将参数封装成PropertyConstraintProvider ,即保存某个propertyName 对应的 约束规则

 

  3) 初始化时候,Rules提供了一个受保护的方法 初始化对象

    protected void initRules() {

       //初始化

    }

 

    创建一个Rule规则时候,可以通过复写initRules()方法,定义一组规则,例如给某个对象定义一组约束规则

    new Rules(ImportDTO.class) {

            @Override

            protected void initRules() {

                add("filePath"new Constraint[] { required() });

                add("fileType"new Constraint[] { required() });

            }

      }

   

5. DelegateRulesSource 对象为一个规则盖里对象

   

    1)添加Rule 规则 ,一下两个方法用于将约束规则保存到属性中

    private DefaultRulesSource delegate;

    public void addRules(Rules rules) {

       delegate.addRules(rules);

    }

 

    public void addRules(String contextId, Rules rules) {

       delegate.addRules(contextId, rules);

    }

   

    其中DefaultRulesSource 中存在一个Map , 通过KEY-VALUE 方式保存约束规则,KEY=ContextId Value=rules. 如果使用不带有contextId的添加方式,contextId将会是默认值DEFAULT_CONTEXT_ID = "default"

 

    2)初始化,DelegateRulesSource初始化时候,额可以通过addRule方法定义一组,即多个规则约束。

protected void doInit() {

       addRules(new Rules(Student.class) {

           protected void initRules() {

              add("code", all(new Constraint[] { maxLength(25)}));

              add("name", all(new Constraint[] { maxLength(25)}));

           }

       });

      

      

       addRules(new Rules(Teacher.class) {

           protected void initRules() {

              add("code", all(new Constraint[] { maxLength(25)}));

              add("name", all(new Constraint[] { maxLength(25)}));

           }

       });

    }

   

  3) 父类DelegateRulesSource提供getRules方法用于获取制定的rule 规则, 可以通过Class,或者contextId 这个标示从Map中取出制定的Rules约束规则

 

   6. RulesValidator 是一个检测器 ,依赖RuleSource ,

   1)可以实现检测某个对象的某个属性是否满足约束规则

   2)实现了接口RichValidator 和 Validator接口

  

public interface RichValidator extends Validator {

    //检测某个对象的某个属性

    ValidationResults validate(Object object, String property);

}

 

public interface Validator {   

    //检测某个对象

    ValidationResults validate(Object object);

}

 

    7、在RCP 中有很多控件需要检测,都可以通过ValidatingFormModel实现,接口方法validate用户检测约束规则。例如:AbstractForm 依赖 ValidatingFormModel,这样就可以通过ValidatingFormModel 实现对FormObject中的对象的属性检测

 

posted @ 2016-03-30 20:27  不及格的飞鱼  阅读(446)  评论(0编辑  收藏  举报