使用Spring MVC的Hibernate-validator组 [英] Hibernate-validator Groups with Spring MVC

查看:138
本文介绍了使用Spring MVC的Hibernate-validator组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用hibernate-validator 4.3.1和Spring MVC 3.2.3。

I'm using hibernate-validator 4.3.1 and Spring MVC 3.2.3.

我的应用程序有一个带有以下属性和注释的bean(我是删除大部分内容使其变得简单):

My application has a bean with the following properties and annotations (I've removed most of them to make it simple):

public class Account {
  @NotEmpty(groups = GroupOne.class)
  private String name;

  @NotEmpty(groups = GroupOne.class)
  private Date creationDate;

  @NotEmpty(groups = GroupTwo.class)
  private String role;

  @NotEmpty(groups = GroupTwo.class)
  private String profile;

  //getters and setters
}

因为它是显示,有两组验证,因为应用程序有一个向导形式,由两个步骤组成:第一步,name和creationDate字段由用户填写,第二步是角色和配置文件字段。

As it is showed, there are two groups of validations because the application has a wizard-form composed of two steps: in the first step the name and creationDate fields are filled by the user and in the second step the role and profile fields.

我添加一些Controller方法作为示例:

I add some Controller methods as example:

@RequestMapping(value = "/account/stepOne", method = "POST")
public String stepOne(@ModelAttribute @Validated(GroupOne.class) Account account, BindingResult bindingResult) {
  //Implementation
}

@RequestMapping(value = "/account/stepTwo", method = "POST")
public String stepTwo(@ModelAttribute @Validated(GroupTwo.class) Account account, BindingResult bindingResult) {
  //Implementation
}

上述方法指定必须由@应用哪些验证验证注释。

The above methods specify which validation has to be applied by @Validated annotation.

直到现在一切都好了很好,但是当我想以另一种形式重用那个bean时,我遇到了一些问题。此表单显示bean的所有字段,接收提交的控制器方法如下:

Until now everything works fine, but I have some problems when I want to reuse that bean in another form. This form displays all the fields of the bean and the controller's method which receives the submit is the following:

@RequestMapping(value = "/account/stepOne", method = "POST")
public String stepOne(@ModelAttribute @Validated Account account, BindingResult bindingResult) {
  //Implementation
}

如您所见,该组的名称已从@Validated注释中删除,因为我想应用定义的所有验证在Account bean中。

As you can see, the name of the group has been removed from the @Validated annotation because I want to apply all the validations defined in Account bean.

然而,它不起作用,使其工作的唯一方法是将默认组添加到bean的属性中,如下所示: / p>

However, it didn't work and the only way to make it work has been to add the Default group to the bean's properties, as so:

public class Account {
  @NotEmpty(groups = {GroupOne.class, Default.class})
  private String name;

  @NotEmpty(groups = {GroupOne.class, Default.class})
  private Date creationDate;

  @NotEmpty(groups = {GroupTwo.class, Default.class})
  private String role;

  @NotEmpty(groups = {GroupTwo.class, Default.class})
  private String profile;

  //getters and setters
}

有没有更优雅的方式来实现这个目标?

Is there a more elegant way to achieve this?

推荐答案

没有更优雅的方式。在不指定组的情况下进行验证将验证默认组。因此,您必须按照描述添加它。唯一的另一种选择是通过 @Validated({GroupOne.class,GroupTwo.class})显式验证您感兴趣的所有组。我想这是你喜欢的味道问题。

There is no more elegant way. Validating without specifying a group will validate the default groups. Hence you have to add it as described. The only other alternative is to explicitly validate all groups you are interested in via @Validated({GroupOne.class, GroupTwo.class}). I guess it is a matter of taste what you prefer.

这篇关于使用Spring MVC的Hibernate-validator组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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