8

有两个问题:

1.Sping/MVC 使用休眠验证器,自定义验证器如何显示消息? 喜欢:使用 Hibernate Validator (JSR 303) 进行跨字段验证

@FieldMatch.List({
    @FieldMatch(fieldName="password",verifyName="passwordVerify",message="password confirm valid!", groups={Default.class})
})
@ScriptAssert(lang="javascript",script="_this.name.equals(_this.verifyCode)")
public class LoginForm {...}

如何在带有资源属性文件的jsp中显示消息?

NotEmpty.loginForm.name="用户名不能为空!" NotEmpty.loginForm.password="密码不能为空!"

2.我想使用spring mvc的组验证器,比如一个用于登录和注册的用户表单

    @FieldMatch.List({
    @FieldMatch(fieldName="password",verifyName="passwordVerify",message="password confirm valid!", groups={Default.class})
})@ScriptAssert(lang="javascript",script="_this.name.equals(_this.verifyCode)",groups={Default.class,LoginChecks.class,RegisterChecks.class})
public class LoginForm {
    @NotEmpty(groups={Default.class,LoginChecks.class,RegisterChecks.class})
    @Size(min=3,max=10,groups={LoginChecks.class,RegisterChecks.class})
    private String name;

    @NotEmpty(groups={Default.class,LoginChecks.class,RegisterChecks.class})
    @Size(max=16,min=5,groups={LoginChecks.class,RegisterChecks.class})
    private String password;

    private String passwordVerify;

    @Email(groups={Default.class,LoginChecks.class,RegisterChecks.class})
    private String email;

    private String emailVerify;
...
}

控制器参数注解为@valid,是否有注解支持按组分组验证器?

第一篇文章:)

4

4 回答 4

3

更新:

Spring 3.1 提供了@Validated 注解,您可以将其用作@Valid 的替代品,并且它接受组。如果您使用的是 Spring 3.0.x,您仍然可以使用此答案中的代码。

原答案:

这绝对是个问题。由于 @Valid 注释不支持组,您必须自己执行验证。这是我们编写的用于执行验证并将错误映射到 BindingResult 中的正确路径的方法。当我们得到一个接受组的 @Valid 注释时,这将是美好的一天。

 /**
 * Test validity of an object against some number of validation groups, or
 * Default if no groups are specified.
 *
 * @param result Errors object for holding validation errors for use in
 *            Spring form taglib. Any violations encountered will be added
 *            to this errors object.
 * @param o Object to be validated
 * @param classes Validation groups to be used in validation
 * @return true if the object is valid, false otherwise.
 */
private boolean isValid( Errors result, Object o, Class<?>... classes )
{
    if ( classes == null || classes.length == 0 || classes[0] == null )
    {
        classes = new Class<?>[] { Default.class };
    }
    Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
    Set<ConstraintViolation<Object>> violations = validator.validate( o, classes );
    for ( ConstraintViolation<Object> v : violations )
    {
        Path path = v.getPropertyPath();
        String propertyName = "";
        if ( path != null )
        {
            for ( Node n : path )
            {
                propertyName += n.getName() + ".";
            }
            propertyName = propertyName.substring( 0, propertyName.length()-1 );
        }
        String constraintName = v.getConstraintDescriptor().getAnnotation().annotationType().getSimpleName();
        if ( propertyName == null || "".equals(  propertyName  ))
        {
            result.reject( constraintName, v.getMessage());
        }
        else
        {
            result.rejectValue( propertyName, constraintName, v.getMessage() );
        }
    }
    return violations.size() == 0;
}

我从关于我们解决方案的博客条目中复制了这个来源。 http://digitaljoel.nerd-herders.com/2010/12/28/spring-mvc-and-jsr-303-validation-groups/

于 2011-08-04T17:21:44.163 回答
1

至于验证组支持内部@Valid注释 - 有一种方法可以做到这一点,我最近发现,它正在重新定义验证 bean 的默认组:

@GroupSequence({TestForm.class, FirstGroup.class, SecondGroup.class})
class TestForm {

    @NotEmpty
    public String firstField;

    @NotEmpty(groups=FirstGroup.class)
    public String secondField; //not validated when firstField validation fails

    @NotEmpty(groups=SecondGroup.class)
    public String thirdField; //not validated when secondField validation fails
}

现在,您仍然可以使用@Valid,但保留验证组的顺序。

于 2011-10-17T10:02:04.783 回答
1

从 Spring 3.1 开始,您可以使用 Spring 实现基于组的验证,@Validated如下所示:

@RequestMapping
public String doLogin(@Validated({LoginChecks.class}) LoginForm) {
    // ...
}

@Validated是 的替代品@Valid

于 2013-04-22T05:09:15.473 回答
1

控制器参数注解为@valid,是否有注解支持按组分组验证器?

现在可以使用@ConvertGroup注释。看看这个http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-group-conversion

于 2013-08-07T15:19:29.793 回答