未调用用于 Crossfield 验证的自定义类级别约束 [英] Custom Class Level Constraint for Crossfield validation not called

查看:25
本文介绍了未调用用于 Crossfield 验证的自定义类级别约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用类级别的自定义注释来实现跨域验证 (JSR-303).然而 isValid 方法没有被调用(而是 initialize 方法).

所以我的问题是:为什么没有为此类级别的验证器调用 isValid 方法?在属性级别定义它是可行的!

我在 JBoss AS 7 和 Websphere AS 8 上尝试过.

这是代码和 JUnit 测试(有效)

Test.java

公共类测试{@org.junit.Testpublic void test() 抛出 ParseException {人人 = 新人();SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMDD");person.setPartyClosingDateFrom(new Date());person.setPartyClosingDateTo(sdf.parse("20120210"));设置<ConstraintViolation<Person>>违规 = Validation.buildDefaultValidatorFactory().getValidator().validate(person);for(ConstraintViolation<Person>违规:违规){System.out.println("消息:- " +违规.getMessage());}}}

DateCompare.java

 import static java.lang.annotation.ElementType.TYPE;导入静态 java.lang.annotation.RetentionPolicy.RUNTIME;导入 java.lang.annotation.Documented;导入 java.lang.annotation.Retention;导入 java.lang.annotation.Target;导入 javax.validation.Constraint;导入 javax.validation.Payload;@Target({ 类型})@保留(运行时间)@Constraint(validatedBy = DateCompareValidator.class)@记录公共@interface DateCompare {/** 第一次约会.*/字符串 firstDate();/** 第二次约会.*/字符串 secondDate();类[]约束()默认{};Class[] groups() 默认{};类[] payload() 默认{};String message() 默认完全错误,伙计!";DateValidator.DateComparisonMode matchMode() 默认DateValidator.DateComparisonMode.EQUAL;}

DateCompareValidator.java

 公共类 DateCompareValidator 实现了 ConstraintValidator{/** 描述验证器应该使用的模式**/私有 DateValidator.DateComparisonMode 比较模式;/** 第一个日期字段名称.*/私人字符串 firstDateFieldName;/** 第二个日期字段名称.*/私人字符串 secondDateFieldName;/** 要使用的消息 **/private String messageKey = "失败";/*** 初始化.** 此方法用于设置参数,即使您不使用任何参数也是必需的** @param constraintAnnotation 约束注解*/@覆盖公共无效初始化(最终日期比较约束注释){this.comparisonMode = constraintAnnotation.matchMode();this.firstDateFieldName = constraintAnnotation.firstDate();this.secondDateFieldName = constraintAnnotation.secondDate();}/*** 检查它是否有效.** @param target 目标* @param context 上下文* @return true, 如果有效*/@覆盖公共布尔 isValid(最终对象目标,最终 ConstraintValidatorContext 上下文){boolean isValid = true;最终日期值Date1 = DateCompareValidator.getPropertyValue(Date.class, this.firstDateFieldName, target);最终日期值Date2 = DateCompareValidator.getPropertyValue(Date.class, this.secondDateFieldName, target);如果(有效){isValid = DateValidator.isValid(valueDate1, valueDate2, this.comparisonMode);} 别的 {//此时比较模式不适合结果,我们必须//设计一个错误信息最终 ResourceBundle messageBundle = ResourceBundle.getBundle("resources.messages");final MessageFormat message = new MessageFormat(messageBundle.getString(this.messageKey));final Object[] messageArguments = { messageBundle.getString(this.messageKey + "." + this.comparisonMode) };//用我们刚刚创建的消息替换默认消息context.disableDefaultConstraintViolation();context.buildConstraintViolationWithTemplate(message.format(messageArguments)).addConstraintViolation();isValid = false;}返回是有效的;}公共静态<T>T getPropertyValue(final Class requiredType, final String propertyName, final Object instance) {如果(requiredType == null){throw new IllegalArgumentException("参数无效.requiredType 不能为空!");}if (propertyName == null) {throw new IllegalArgumentException("参数无效.PropertyName 不能为空!");}如果(实例 == 空){throw new IllegalArgumentException("参数无效.对象实例不能为空!");}T returnValue = null;尝试 {final PropertyDescriptor 描述符 = new PropertyDescriptor(propertyName, instance.getClass());final Method readMethod = descriptor.getReadMethod();if (readMethod == null) {throw new IllegalStateException("Property '" + propertyName + "' of " + instance.getClass().getName()+ " 不可读!");}if (requiredType.isAssignableFrom(readMethod.getReturnType())) {尝试 {final Object propertyValue = readMethod.invoke(instance);returnValue = requiredType.cast(propertyValue);} 捕获(最终异常 e){e.printStackTrace();//无法调用readMethod}}} catch (final IntrospectionException e) {throw new IllegalArgumentException("Property '" + propertyName + "' is not defined in "+ instance.getClass().getName() + "!", e);}返回返回值;}

DateValidator.java

 公共类 DateValidator {/*** 枚举 DateComparisonMode.** 确定使用哪种验证类型*/公共枚举 DateComparisonMode {/** 给定的日期必须在引用的日期之前 */前,/** 给定的日期必须是 BEFORE_OR_EQUAL 引用的日期 */BEFORE_OR_EQUAL,/** 给定的日期必须等于引用的日期 */平等的,/** 给定的日期必须是 AFTER_OR_EQUAL 引用的日期 */AFTER_OR_EQUAL,/** 给定的日期必须在引用的日期之后 */后;}/*** 根据给定的比较模式比较 2 个日期值.** @param baseDate 基准日期* @paramvaluationDate 估价日期* @param compareMode 比较模式* @return true, 如果有效*/public static boolean isValid(final Date baseDate, final Date ratingDate, final DateComparisonMode compareMode) {//两个日期的时间值都将设置为 00:00:0000最终日期 compValuationDate = DateValidator.convertDate(valuationDate);最终日期 compBaseDate = DateValidator.convertDate(baseDate);//比较值最终 int 结果 = compValuationDate.compareTo(compBaseDate);//将结果与comparisonMode 匹配并返回true//如果规则满足开关(结果){情况1:if (comparisonMode == DateComparisonMode.BEFORE) {返回真;}if (comparisonMode == DateComparisonMode.BEFORE_OR_EQUAL) {返回真;}休息;案例0:if (comparisonMode == DateComparisonMode.BEFORE_OR_EQUAL) {返回真;}if (comparisonMode == DateComparisonMode.EQUAL) {返回真;}if (comparisonMode == DateComparisonMode.AFTER_OR_EQUAL) {返回真;}休息;情况1:if (comparisonMode == DateComparisonMode.AFTER) {返回真;}if (comparisonMode == DateComparisonMode.AFTER_OR_EQUAL) {返回真;}休息;默认:返回假;//不应该发生....}返回假;}/*** 转换日期.** 将指定日期的时间值设置为 00:00:0000** @param t t* @return 日期*/私有静态日期转换日期(最终日期 t){最终日历日历 = Calendar.getInstance();日历.setTime(t);日历.设置(日历.HOUR_OF_DAY,0);日历.设置(日历.分钟,0);日历.设置(日历.SECOND,0);日历.设置(日历.MILLISECOND,0);返回 (calendar.getTime());}

特别是属性检索取自这篇文章问题

解决方案

JSF 2.0 不调用类级别验证约束.来自 JSF 验证:><块引用>

JSF 2 提供与 JSR-303 约束的内置集成.当你在您的应用程序中使用 bean 验证,JSF 自动使用UIInput 值引用的 bean 的约束.

必须手动调用,也可以试试Seam Faces 具有扩展名

I am trying to implement a crossfield validation (JSR-303) with a custom annotation on class level. However the isValid method is not called (but the initialize method).

So my question is: Why is the isValid method not being called for this class level validator? Defining it on property level works!

I tried it on JBoss AS 7 and Websphere AS 8.

Here is the code and a JUnit test (which works)

Test.java

public class Test {

@org.junit.Test
public void test() throws ParseException {
    Person person = new Person();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMDD");
    person.setPartyClosingDateFrom(new Date());
    person.setPartyClosingDateTo(sdf.parse("20120210"));
    Set<ConstraintViolation<Person>> violations = Validation.buildDefaultValidatorFactory().getValidator().validate(person);
    for(ConstraintViolation<Person> violation : violations) {
        System.out.println("Message:- " + violation.getMessage());
    }
}
  }

DateCompare.java

 import static java.lang.annotation.ElementType.TYPE;
 import static java.lang.annotation.RetentionPolicy.RUNTIME;
 import java.lang.annotation.Documented;
 import java.lang.annotation.Retention;
 import java.lang.annotation.Target;
 import javax.validation.Constraint;
 import javax.validation.Payload;

 @Target({ TYPE})
 @Retention(RUNTIME)
 @Constraint(validatedBy = DateCompareValidator.class)
 @Documented
 public @interface DateCompare {

/** First date. */
String firstDate();

/** Second date. */
String secondDate();

Class<?>[] constraints() default {};

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};

String message() default "totally wrong, dude!";

DateValidator.DateComparisonMode matchMode() default 
    DateValidator.DateComparisonMode.EQUAL;
 }

DateCompareValidator.java

 public class DateCompareValidator implements ConstraintValidator<DateCompare, Object>    {

/** describes the mode the validator should use**/
private DateValidator.DateComparisonMode comparisonMode;

/** The first date field name. */
private String firstDateFieldName;

/** The second date field name. */
private String secondDateFieldName;

/** the message to be used **/
private String messageKey = "failure";

/**
 * Initialize.
 * 
 * This method is used to set the parameters ans is REQUIRED even if you don't use any parameters
 * 
 * @param constraintAnnotation the constraint annotation
 */
@Override
public void initialize(final DateCompare constraintAnnotation) {
    this.comparisonMode = constraintAnnotation.matchMode();
    this.firstDateFieldName = constraintAnnotation.firstDate();
    this.secondDateFieldName = constraintAnnotation.secondDate();

}

/**
 * Checks if it is valid.
 * 
 * @param target the target
 * @param context the context
 * @return true, if is valid
 */
@Override
public boolean isValid(final Object target, final ConstraintValidatorContext context) {
    boolean isValid = true;

    final Date valueDate1 = DateCompareValidator.getPropertyValue(Date.class, this.firstDateFieldName, target);
    final Date valueDate2 = DateCompareValidator.getPropertyValue(Date.class, this.secondDateFieldName, target);
    if (isValid) {
        isValid = DateValidator.isValid(valueDate1, valueDate2, this.comparisonMode);
    } else {
        // at this point comparisonMode does not fit tp the result and we have to
        // design an error Message
        final ResourceBundle messageBundle = ResourceBundle.getBundle("resources.messages");
        final MessageFormat message = new MessageFormat(messageBundle.getString(this.messageKey));
        final Object[] messageArguments = { messageBundle.getString(this.messageKey + "." + this.comparisonMode) };

        // replace the default-message with the one we just created
        context.disableDefaultConstraintViolation();
        context.buildConstraintViolationWithTemplate(message.format(messageArguments)).addConstraintViolation();
        isValid = false;
    }
    return isValid;
}


public static <T> T getPropertyValue(final Class<T> requiredType, final String propertyName, final Object instance) {
    if (requiredType == null) {
        throw new IllegalArgumentException("Invalid argument. requiredType must NOT be null!");
    }
    if (propertyName == null) {
        throw new IllegalArgumentException("Invalid argument. PropertyName must NOT be null!");
    }
    if (instance == null) {
        throw new IllegalArgumentException("Invalid argument. Object instance must NOT be null!");
    }
    T returnValue = null;
    try {
        final PropertyDescriptor descriptor = new PropertyDescriptor(propertyName, instance.getClass());
        final Method readMethod = descriptor.getReadMethod();
        if (readMethod == null) {
            throw new IllegalStateException("Property '" + propertyName + "' of " + instance.getClass().getName()
                    + " is NOT readable!");
        }
        if (requiredType.isAssignableFrom(readMethod.getReturnType())) {
            try {
                final Object propertyValue = readMethod.invoke(instance);
                returnValue = requiredType.cast(propertyValue);
            } catch (final Exception e) {
                e.printStackTrace(); // unable to invoke readMethod
            }
        }
    } catch (final IntrospectionException e) {
        throw new IllegalArgumentException("Property '" + propertyName + "' is NOT defined in "
                + instance.getClass().getName() + "!", e);
    }
    return returnValue;
}

DateValidator.java

  public class DateValidator {

/**
 * The Enum DateComparisonMode.
 * 
 * Determins which Type of validation is used
 */
public enum DateComparisonMode {

    /** the given Date must be BEFORE the referenced Date */
    BEFORE,

    /** the given Date must be BEFORE_OR_EQUAL the referenced Date */
    BEFORE_OR_EQUAL,

    /** the given Date must be EQUAL the referenced Date */
    EQUAL,

    /** the given Date must be AFTER_OR_EQUAL the referenced Date */
    AFTER_OR_EQUAL,

    /** the given Date must be AFTER the referenced Date */
    AFTER;
}

/**
 * Compare 2 Date Values based on a given Comparison Mode.
 * 
 * @param baseDate the base date
 * @param valuationDate the valuation date
 * @param comparisonMode the comparison mode
 * @return true, if is valid
 */
public static boolean isValid(final Date baseDate, final Date valuationDate, final DateComparisonMode comparisonMode) {
    // Timevalue of both dates will be set to 00:00:0000
    final Date compValuationDate = DateValidator.convertDate(valuationDate);
    final Date compBaseDate = DateValidator.convertDate(baseDate);

    // compare the values
    final int result = compValuationDate.compareTo(compBaseDate);

    // match the result to the comparisonMode and return true
    // if rule is fulfilled
    switch (result) {
    case -1:
        if (comparisonMode == DateComparisonMode.BEFORE) {
            return true;
        }
        if (comparisonMode == DateComparisonMode.BEFORE_OR_EQUAL) {
            return true;
        }

        break;

    case 0:
        if (comparisonMode == DateComparisonMode.BEFORE_OR_EQUAL) {
            return true;
        }
        if (comparisonMode == DateComparisonMode.EQUAL) {
            return true;
        }
        if (comparisonMode == DateComparisonMode.AFTER_OR_EQUAL) {
            return true;
        }
        break;

    case 1:
        if (comparisonMode == DateComparisonMode.AFTER) {
            return true;
        }
        if (comparisonMode == DateComparisonMode.AFTER_OR_EQUAL) {
            return true;
        }

        break;
    default:
        return false; // should not happen....
    }
    return false;
}

/**
 * Convert date.
 * 
 * sets the time Value of a given Date filed to 00:00:0000
 * 
 * @param t the t
 * @return the date
 */
private static Date convertDate(final Date t) {
    final Calendar calendar = Calendar.getInstance();
    calendar.setTime(t);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    return (calendar.getTime());
}

Especially the property retrieval was taken from this post question

解决方案

JSF 2.0 doesn't call class level validation constraints. From JSF validation:

JSF 2 provides built-in integration with JSR-303 constraints. When you are using bean validation in your application, JSF automatically uses the constraints for beans that are referenced by UIInput values.

You have to call it manually, or you can try Seam Faces which has an extension <f:validateBean>

这篇关于未调用用于 Crossfield 验证的自定义类级别约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆