我正在尝试在Spring Webflow 2.4中实现部分验证。根据他们的参考手册,验证应该非常简单地使用组进行:
@NotNull
@Size(min = 2, max = 30, groups = State1.class)
private String name;
根据我的理解,State1应该是应该验证模型的视图状态的ID。因此,flow.xml中此视图状态的定义如下所示:
<view-state id="state1" model="modelObject"/>
我试图将State1定义为我的模型对象的内部类,但没有成功。
Webflow参考文献没有提供部分验证的完整手册,所以我的问题是:我错过了什么吗?有没有人使用JSR303组使用部分验证的经验?
谢谢,Shimon
答案 0 :(得分:3)
我想我现在可以回答我自己的问题:)
问题的根源在于两件事:
Group1应该是模型对象的内部接口。所以模型对象类看起来应该是这样的:
public clas ModelObject{
@NotEmpty(groups=Group1.class)
private String field1;
public interface Group1{}
}
名称od validation-hint应该用单引号
validation-hints="'group1'"
答案 1 :(得分:0)
“根据我的理解,State1应该是应该验证模型的视图状态的ID。”
这里的组不是指视图状态ID。它是由模型对象实现的内部类或父类或接口。
要实现JSR-303部分验证,在SWF 2.4以后(这是SWF开始支持它的版本),您需要将validation-hints指定为:
<view-state id="someView" model="modelObject" validation-hints="group1,group2">
其中group1,group2可以是模型类型modelObject中的内部类,也可以是由modelObject实现的父类型或接口。
根据文档here:
Each hint can be an inner Class either in the model type or its parent types.
For example, given org.example.MyModel with inner type Group1 and Group2 you
can specify the hints "group1", "group2" or both "group1,group2". A hint can
also be a fully qualified class name. The hint "default" indicates the default
validation group, i.e. javax.validation.groups.Default. Also, the validation-hints
property can be an expression that resolves to a String or an Object[] containing
Class based hints.