我正在尝试使用类级别的自定义注释来实现跨域验证(JSR-303)。但是,不会调用isValid方法(而是调用initialize方法)。
所以我的问题是:为什么不为该类级别的验证程序调用isValid方法?在属性级别定义它是可行的!
我在JBoss AS 7和Websphere AS 8上尝试过。
这是代码和一个JUnit测试(有效)
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());
}
特别是属性检索是从这个帖子问题中获取的
JSF 2.0不会调用类级别的验证约束。通过JSF验证:
JSF 2提供了与JSR-303约束的内置集成。在应用程序中使用bean验证时,JSF会自动使用
UIInput
值引用的bean的约束。
您必须手动调用它,也可以尝试使用带有扩展名的Seam
Faces<f:validateBean>
我有一个自定义注释@UniqueModel,它由ConstraintValidator验证: 问题是,我需要在调用存储库的safe()-方法之前进行验证,否则字段注入将无法工作。 因此,我创建了一个带有@Valid注释的委托方法,以便在以下情况之前强制进行唯一验证: 不幸的是,这不起作用,似乎@Valid注释被Spring忽略了。 我如何确保验证的正确时间?
我有一个像下面这样的模型对象,带有自定义约束验证器。自定义验证器检查是否填充了fileName或小时。 有一种方法将此作为输入,它验证所有以下条件 > 条件不为空(通过默认验证器) criteria.id不为空(通过默认验证器) criteria.name不为空(通过默认验证器) 标准文件名或小时不为空(通过自定义验证器) 空评估(@NotNull@有效标准标准){} 现在,当我为这个模型类编写单
我已经为此斗争了好几天,我已经阅读了这里的所有讨论,但没有解决方案...... 我有一个自定义约束… 独一无二.java 这是由UniqueConstraintValidator使用的.java 现在,我的模型有这个注释,我需要在其中验证字段的唯一性 现在,我知道自定义注释在更新现有对象时不起作用,但是现在我需要了解它是如何工作的。假设我们想要添加一个新对象。 当我试图保存一个已经存在的值时,我得
TLDR:我想要在单独的模块中单独的自定义bean验证定义及其ConstraintValidator实现。为此,我必须使用ConstraintMmap手动注册。它适用于带注释的类。但是定义的绑定不共享/可用于通过validation-constraints.xml.定义的验证如何修复?我试图调试它,以找出它在哪里初始化以及为什么会出现问题,但初始化这些远非易事。 动机: 一) 分离模块:如果API
配置类 注释类 验证程序类 我有一个属性为的类,我还使用了其他注释,比如和,最后两个可以工作,但我的自定义注释不工作。 你能帮助理解为什么Spring不调用自定义验证器吗?
我创建了一个自定义约束验证器,用于检查用户名的验证,验证器正在访问数据库并检查记录是否已存在以及是否符合正则表达式。我收到相当奇怪的错误,因为如果用户名已经存在,验证器可以正常工作,但是当输入有效输入(不存在的用户名)时,它会抛出一个 NullPointerException。 我放置了一个计数器和一些语句来跟踪应用程序崩溃的位置,我将在堆栈跟踪中包括这些。应用程序似乎正在调用和方法两次,第二次是