【问题标题】:How to make webflow view call only one validator如何让 webflow 视图只调用一个验证器
【发布时间】:2014-05-31 00:28:23
【问题描述】:

我有这个视图的网络流:

 <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");
    }
}

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

在 validate() 中(用于视图 displayNameView)newRecipe 总是会有空字段描述和类别。

【问题讨论】:

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


    【解决方案1】:

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

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

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

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

    您可以使用提及相同 here 的 web 流文档。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-02
      • 2016-03-07
      • 2019-07-08
      • 2011-04-30
      • 1970-01-01
      • 2015-01-03
      相关资源
      最近更新 更多