你能在运行时更改注释消息吗? [英] Can you change an annotation message at run time?

查看:68
本文介绍了你能在运行时更改注释消息吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的注释中包含一个动态消息,该消息根据传递给它的其他变量中的值来更改文本的主体。我设置了默认消息,但是当设置了某个指示符时,我想显示不同的消息。这可能吗?



这是我的注释 -

  @Target({TYPE,ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = FieldMatchValidator.class)
@Documented
public @interface FieldMatch
{
Class<? > [] groups()默认{};
等级<?扩展Payload> [] payload()default {};
String first();
String second();
String third()default;
String match()默认为true;
String message()默认{error.theseValuesDontMatch};

/ **
*定义几个< code> @ FieldMatch< / code>同一元素上的注释
*
* @see FieldMatch
* /
@Target({TYPE,ANNOTATION_TYPE})
@Retention(RUNTIME)
@Documented @interface List
{
FieldMatch [] value();
}
}

这是注释使用的验证器类 -

 公共类FieldMatchValidator实现ConstraintValidator< FieldMatch,Object> 
{
private String firstFieldName;
private String secondFieldName;
private String thirdFieldName;
private String match;
私有字符串消息;

@Override
public void initialize(FieldMatch constraintAnnotation)
{
firstFieldName = constraintAnnotation.first();
secondFieldName = constraintAnnotation.second();
thirdFieldName = constraintAnnotation.third();
match = constraintAnnotation.match();
if(match!= null&&!Boolean.getBoolean(match)){
message =error.theseValuesMustNotMatch;
}
}

@Override
public boolean isValid(final Object value,final ConstraintValidatorContext context)
{
try
{
final Object firstObj = BeanUtils.getProperty(value,firstFieldName);
final Object secondObj = BeanUtils.getProperty(value,secondFieldName);
final Object thirdObj = BeanUtils.getProperty(value,thirdFieldName);
final String same = BeanUtils.getProperty(value,match);

boolean valid = false;
if(same!= null&& Boolean.getBoolean(same)){
if(。equals(thirdObj)){
valid = firstObj == null&& ; secondObj == null || firstObj!= null&& firstObj.equals(secondObj);
}
else {
valid = firstObj!= null&& firstObj.equals(secondObj)&& firstObj.equals(thirdObj);
}
}
else {
if(。equals(thirdObj)){
valid = firstObj == null&& secondObj == null || firstObj!= null&& !firstObj.equals(secondObj);
}
else {
valid = firstObj!= null&& !(firstObj.equals(secondObj)&& firstObj.equals(thirdObj));
}
}
返回有效;
}
catch(最终忽略异常)
{
//忽略
}
返回true;
}
}

我最感兴趣的是代码如果(匹配!= null&&!Boolean.getBoolean(match)){
message =password.error.theseValuesMustNotMatch;
}


解决方案

以下是我的能力这样做 -

  @Override 
public void initialize(FieldMatch constraintAnnotation)
{
firstFieldName = constraintAnnotation.first();
secondFieldName = constraintAnnotation.second();
thirdFieldName = constraintAnnotation.third();
match = constraintAnnotation.match();

//在初始化时设置消息变量
if(true.equals(match)){
message = constraintAnnotation.message();
}
else {
message ={password.error.threeQuestionsSameAnswer};}
}

@Override
public boolean isValid (final Object value,final ConstraintValidatorContext context)
{
Object firstObj = null;
Object secondObj = null;
Object thirdObj = null;

//禁用现有违规消息
context.disableDefaultConstraintViolation();
//构建新的违规消息并添加它
context.buildConstraintViolationWithTemplate(message).addConstraintViolation();

等.........
}


I'm trying to include a dynamic message in my annotation that changes the main body of the text based on the values that are found in the other variables that are passed to it. I set a default message, but when a certain indicator is set, I want to display a different message. Is this possible?

Here's my annotation -

@Target({TYPE, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = FieldMatchValidator.class)
@Documented
public @interface FieldMatch
{
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
    String first();
    String second();
    String third() default "";
    String match() default "true";
    String message() default "{error.theseValuesDontMatch}";

    /**
     * Defines several <code>@FieldMatch</code> annotations on the same element
     *
     * @see FieldMatch
     */
    @Target({TYPE, ANNOTATION_TYPE})
    @Retention(RUNTIME)
    @Documented @interface List
    {
        FieldMatch[] value();
    }
}

Here's the validator class used by the annotation -

public class FieldMatchValidator implements ConstraintValidator<FieldMatch, Object>
{
    private String firstFieldName;
    private String secondFieldName;
    private String thirdFieldName;
    private String match;
    private String message;

    @Override
    public void initialize(FieldMatch constraintAnnotation)
    {
        firstFieldName = constraintAnnotation.first();
        secondFieldName = constraintAnnotation.second();
        thirdFieldName = constraintAnnotation.third();
        match = constraintAnnotation.match();
        if(match != null && !Boolean.getBoolean(match)){
            message = "error.theseValuesMustNotMatch";
        }
    }

    @Override
    public boolean isValid(final Object value, final ConstraintValidatorContext context)
    {
        try
        {
            final Object firstObj = BeanUtils.getProperty(value, firstFieldName);
            final Object secondObj = BeanUtils.getProperty(value, secondFieldName);
            final Object thirdObj = BeanUtils.getProperty(value, thirdFieldName);
            final String same = BeanUtils.getProperty(value, match);

            boolean valid = false;
            if(same != null && Boolean.getBoolean(same)){
                if("".equals(thirdObj)){
                    valid = firstObj == null && secondObj == null || firstObj != null && firstObj.equals(secondObj) ;
                }
                else{
                    valid = firstObj != null && firstObj.equals(secondObj) && firstObj.equals(thirdObj) ;   
                }
            }
            else{
                if("".equals(thirdObj)){
                    valid = firstObj == null && secondObj == null || firstObj != null && !firstObj.equals(secondObj) ;
                }
                else{
                    valid = firstObj != null && !(firstObj.equals(secondObj) && firstObj.equals(thirdObj)) ;   
                }
            }
            return valid ;
        }
        catch (final Exception ignore)
        {
            // ignore
        }
        return true;
    }
}

The piece I'm most interested in is the code that reads -

    if(match != null && !Boolean.getBoolean(match)){
        message = "password.error.theseValuesMustNotMatch";
    }

解决方案

Here's how I was able to do this -

@Override
public void initialize(FieldMatch constraintAnnotation)
{
    firstFieldName = constraintAnnotation.first();
    secondFieldName = constraintAnnotation.second();
    thirdFieldName = constraintAnnotation.third();
    match = constraintAnnotation.match();

    //set a message variable on initialization    
    if("true".equals(match)){
        message = constraintAnnotation.message();
    }
    else{
        message = "{password.error.threeQuestionsSameAnswer}";}
}

@Override
public boolean isValid(final Object value, final ConstraintValidatorContext context)
{
    Object firstObj = null;
    Object secondObj = null;
    Object thirdObj = null;

    //disable existing violation message
    context.disableDefaultConstraintViolation();
    //build new violation message and add it
    context.buildConstraintViolationWithTemplate(message).addConstraintViolation();

etc.........
}

这篇关于你能在运行时更改注释消息吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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