如何使webflow视图只调用一个验证器

时间:2014-04-16 08:43:24

标签: java spring validation spring-webflow spring-webflow-2

我有这个观点的网络流程:

 <var name="newRecipe" class="springapp.service.NewRecipe" />

 <view-state id="displayNameView" view="/WEB-INF/jsp/add_name.jsp" model="newRecipe">
     <transition on="nameEntered" to="displayDescriptionView" />
 </view-state>

和验证者:

@Component("newRecipeValidator")
public class NewRecipeValidator implements Validator {

    @Override
    public boolean supports(@SuppressWarnings("rawtypes") final Class clazz) {
        return NewRecipe.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(final Object obj, final Errors errors) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.not-specified-name", "Pole wymagane.");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "description", "error.not-specified-description", "Pole wymagane.");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "category", "error.not-specified-category", "Pole wymagane.");
    }

    public void validateDisplayNameView(final NewRecipe obj, final ValidationContext context) {
        System.out.println("okej");
    }
}

我的问题是,当网络流尝试验证视图displayNameView时,它会调用validateDisplayNameView(),然后调用validate()。

在validate()中(对于view displayNameView),newRecipe总是会有空字段描述和类别。

1 个答案:

答案 0 :(得分:1)

这是实现Validator进行视图验证时的默认行为。

如果您在Validator实现中重写了validate方法,并且在视图状态中启用了验证(例如,displayNameView),则首先调用validateDisplayNameView方法,然后调用validate方法。

仅当要对流中的所有视图执行某些常见验证时,才会覆盖此默认验证方法。 如果不是,我不会覆盖或保持为空:

    public void validate(Object obj, Errors err){};

您可以弹出提及相同here的Web流量文档。

在您的场景中,如果要检查displayNameView的validate方法中的语句,请将它们移动到validateDisplayNameView方法。