Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 使用HibernateValidator进行跨域验证时,不会显示任何错误消息_Spring_Jsp_Spring Mvc_Bean Validation_Hibernate Validator - Fatal编程技术网

Spring 使用HibernateValidator进行跨域验证时,不会显示任何错误消息

Spring 使用HibernateValidator进行跨域验证时,不会显示任何错误消息,spring,jsp,spring-mvc,bean-validation,hibernate-validator,Spring,Jsp,Spring Mvc,Bean Validation,Hibernate Validator,我正在验证表单上的两个字段“password”和“confirmPassword”,以便按照中的指定使用。以下是约束描述符(验证器接口) 下面是约束验证器(实现类) 更新 这一切都正常工作,并进行了预期的验证。剩下的一件事是在@FieldMatch中的TempBean类上方显示指定的错误消息,即只有一个问题:当验证冲突发生时,如何在JSP页面上显示错误消息 (在TempBean类中的password和confirmPassword两个字段上的注释@NotEmpty都起作用,并在发生冲突时显示指

我正在验证表单上的两个字段“password”和“confirmPassword”,以便按照中的指定使用。以下是约束描述符(验证器接口)

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


更新

这一切都正常工作,并进行了预期的验证。剩下的一件事是在
@FieldMatch
中的
TempBean
类上方显示指定的错误消息,即只有一个问题:当验证冲突发生时,如何在JSP页面上显示错误消息

(在
TempBean
类中的
password
confirmPassword
两个字段上的注释
@NotEmpty
都起作用,并在发生冲突时显示指定的消息,
@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>




你能试试你的isValid方法吗?(这在live project中确实对我有用):

此外,我还看到您正在使用对象实现ConstraintValidator接口。它应该是表单中的支持对象:

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

因此,您应该像这样:

 implements ConstraintValidator<FieldMatch, TempBean>

在isValida内部,获取它的方法与第一个和第二个字段名相同,并使用它。

没有执行验证的原因是因为问题“所有内容都在同一个包
包validatorbeans;
”中的这句话。HibernateValidator需要约束描述符(注释)和约束验证器(实现类)的公共定义,我在同一个包中声明了它们,因此,默认修饰符no modifier应用于它们(因为不太可能在同一个包中将多个类或接口声明为public)。但是仍然存在一个问题。它仍然没有显示注释指定的错误消息。有人知道即使正确执行了验证,它也没有显示错误消息的原因吗?好的,我认为您应该发布关于包专用验证器的解决方案作为答案。至于错误消息,可能是一个问题lem的形式是:错误声明,问题中没有指定。在我的第一次评论中,这是一个错误(在最后一行中)“因为在同一个包中不太可能将多个类或接口声明为public”。它应该在同一个文件中,而不是在同一个包中。对于这个错误,我真的很抱歉。您是否能够获得错误消息来显示以进行简单验证?更重要的是,您使用的是什么servlet和JSP库?根据您的JSP代码片段和标记,我猜是Spring MVC。经过尝试,它成功了,先生!提供赏金要求it’从现在起几个小时后,我会在系统允许的情况下将其授予您的答案。请再说一件事,使用此方法本身
cvc.buildConstraintViolationWithTemplate(“两个字段中的密码必须匹配”)。addNode(密码)。addConstraintViolation();
,我已指定了错误消息。是否有方法获取
@FieldMatch.List({})中指定的消息
?不管是否没有办法。这是一件小事。对我来说,根据你的回答知道它工作很好是一件好事。非常感谢。现在我的整个应用程序都工作得很好。非常感谢,先生!@Tiny很高兴我能帮上忙!@Eugene有可能实现国际化吗?\@Harmeethinghtara:那只是需要从本地化/国际化属性文件中指定消息键,以替换实际消息,如
@FieldMatch.List({@FieldMatch(first=“password”,second=“confirmPassword”,message=“{message.key}”,groups{TempBean.ValidationGroup.class})
,如问题(
message=“{message.key}”)所述。
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                
}
<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>
 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;
}
 implements ConstraintValidator<FieldMatch, TempBean>
  Class<? extends Payload>[] payload() default {};

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

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

/**
  @return the Error Message
 */
String 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);
}