validate with annotation

setp
1:define
    @Retention(RetentionPolicy.RUNTIME)
    public @interface RequestValidate {
        RequestValidateItem[] value();
    }

    @Retention(RetentionPolicy.RUNTIME)
    public @interface RequestValidateItem {
        public String parameter() default "";
        public static final String TYPE_DOUBLE = "#DOUBLE";
    }
2:use point
    @RequestValidate({
        @RequestValidateItem(parameter="createTime",required=true,caption="创建时间",msg="不能为空",type=RequestValidateItem.TYPE_DATE),
        @RequestValidateItem(parameter="endTime",required=true,caption="创建时间",msg="不能为空",type=RequestValidateItem.TYPE_DATE)
    })
3:proccess logic
            targetMethod = this.getClass().getMethod(targetMethodName);
            if (targetMethod.isAnnotationPresent(RequestValidate.class)) {
                StringBuffer retObjMsg = new StringBuffer();
                Annotation[] annotations = targetMethod.getAnnotations();
                for (Annotation annotation : annotations) {
                    if (annotation instanceof RequestValidate) {
                        RequestValidate rv = (RequestValidate) annotation;
                        for(RequestValidateItem rvi : rv.value()){
                            String retValidate = validate(rvi);//detail logic
                            if (null != retValidate) {
                                retObjMsg.append(retValidate + "   ");
                            }
                        }
                    }
                if (retObjMsg.length() > 0)
                    return createMsgBox(retObjMsg.toString());
            }
            retObj = targetMethod.invoke(this);


test method:use po test annotation
    1:new plugmodule:<property name="autoRegisterComponent" value="true"/>
    2:new DemoPO extends StatePresentationObject
    3:<service-using
            serviceUuid="hippo.plugmodule.services.presentation"
            description="注册必要的入口页面">
            <WebPresentation namespace="hkx"
                autoRegisterResource="true" autoRegisterPO="true"
                presentationObjectPackage="com.woaika.framework.test.hkx.prez">
            </WebPresentation>
    </service-using>

你可能感兴趣的:(annotation)