使用HibernateValidator进行交叉字段验证不会显示任何错误消息

Tin*_*iny 14 spring jsp spring-mvc hibernate-validator bean-validation

我正在使用此答案中HibernateValidator指定的表格上的两个字段"password"和"confirmPassword"进行验证.以下是约束描述符(验证器接口).

package constraintdescriptor;

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

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

    /**
     * @return The first field
     */
    String first();

    /**
     * @return The second field
     */
    String second();

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

以下是约束验证器(实现类).


package constraintvalidator;

import constraintdescriptor.FieldMatch;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import org.apache.commons.beanutils.BeanUtils;

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

    public void initialize(final FieldMatch constraintAnnotation) {
        firstFieldName = constraintAnnotation.first();
        secondFieldName = constraintAnnotation.second();
        //System.out.println("firstFieldName = "+firstFieldName+"   secondFieldName = "+secondFieldName);
    }

    public boolean isValid(final Object value, final ConstraintValidatorContext cvc) {
        try {
            final Object firstObj = BeanUtils.getProperty(value, firstFieldName );
            final Object secondObj = BeanUtils.getProperty(value, secondFieldName );
            //System.out.println("firstObj = "+firstObj+"   secondObj = "+secondObj);
            return firstObj == null && secondObj == null || firstObj != null && firstObj.equals(secondObj);
        }
        catch (final Exception e) {
            System.out.println(e.toString());
        }
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是与JSP页面一起映射的验证器bean(commandName="tempBean"<form:form></form:form>标记一起指定).

package validatorbeans;

import constraintdescriptor.FieldMatch;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;

@FieldMatch.List({
    @FieldMatch(first = "password", second = "confirmPassword", message = "The password fields must match", groups={TempBean.ValidationGroup.class})
})

public final class TempBean
{        
    @NotEmpty(groups={ValidationGroup.class}, message="Might not be left blank.")
    private String password;
    @NotEmpty(groups={ValidationGroup.class}, message="Might not be left blank.")
    private String confirmPassword;

    public interface ValidationGroup {};

    //Getters and setters                
}
Run Code Online (Sandbox Code Playgroud)

UPDATE

这一切都正常工作,并进行验证.剩下的一件事就是显示未显示的TempBean类上面的指定错误消息,@FieldMatch只有一个问题: 如何在发生验证违规时在JSP页面上显示错误消息?

(注释@NotEmpty两个字段password,并confirmPasswordTempBean类的作品,并显示在违反规定的消息,事情是不是有发生@FieldMatch).

我正在使用基于此博客中指定的问题的验证组,它运行良好,不会导致显示错误消息的中断(看起来似乎如此).


在JSP页面上,这两个字段指定如下.

<form:form id="mainForm" name="mainForm" method="post" action="Temp.htm" commandName="tempBean">

    <form:password path="password"/>
    <font style="color: red"><form:errors path="password"/></font><br/>

    <form:password path="confirmPassword"/>
    <font style="color: red"><form:errors path="confirmPassword"/></font><br/>

</form:form>
Run Code Online (Sandbox Code Playgroud)

Eug*_*ene 21

你可以试试你的isValid方法吗?(这对我来说在现场项目中肯定有用):

 public boolean isValid(final Object value, final ConstraintValidatorContext cvc){
    boolean toReturn = false;

    try{
        final Object firstObj = BeanUtils.getProperty(value, firstFieldName );
        final Object secondObj = BeanUtils.getProperty(value, secondFieldName );

        //System.out.println("firstObj = "+firstObj+"   secondObj = "+secondObj);

        toReturn = firstObj == null && secondObj == null || firstObj != null && firstObj.equals(secondObj);
    }
    catch (final Exception e){
        System.out.println(e.toString());
    }
    //If the validation failed
    if(!toReturn) {
        cvc.disableDefaultConstraintViolation();
        //In the initialiaze method you get the errorMessage: constraintAnnotation.message();
        cvc.buildConstraintViolationWithTemplate(errorMessage).addNode(firstFieldName).addConstraintViolation();
    }
    return toReturn;
}
Run Code Online (Sandbox Code Playgroud)

另外,我看到你正在用字符实现带有Object的ConstraintValidator接口.它应该是您从表单中获得的支持对象:

tempBean //实际上是在commandName中指定的那个.

所以你的实现应该是这样的:

 implements ConstraintValidator<FieldMatch, TempBean>
Run Code Online (Sandbox Code Playgroud)

这可能不是这里的问题,但作为未来的参考,它应该是这样的.

UPDATE

在FieldMatch接口/注释中,您有两种方法:第一种和第二种,例如,再添加一种名为errorMessage的方法:

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

/**
 * @return The first field
 */
String first();

/**
 * @return The second field
 */
String second();

/**
  @return the Error Message
 */
String errorMessage
Run Code Online (Sandbox Code Playgroud)

从Validation类中查看你的方法 - 你得到第一个和第二个字段名称.所以只需添加errorMessage,例如:

  private String firstFieldName;
  private String secondFieldName;
  //get the error message name
  private String errorMessagename; 
public void initialize(final FieldMatch constraintAnnotation)
{
    firstFieldName = constraintAnnotation.first();
    secondFieldName = constraintAnnotation.second();
    errorMessageNAme = constraintAnnotation.errorMessage(); 

    //System.out.println("firstFieldName = "+firstFieldName+"   secondFieldName = "+secondFieldName);
}
Run Code Online (Sandbox Code Playgroud)

在内部isValida得到它,就像你对第一和第二个字段名称所做的那样并使用它.

  • @HarmeetSinghTaara:只需要从本地化/国际化属性文件中指定消息密钥,替换实际消息,例如`@ FieldMatch.List({@ FieldMatch(first ="password",second ="confirmPassword",message =" {message.key}",groups {TempBean.ValidationGroup.class})})``如问题所示(`message ="{message.key}"`). (2认同)