Spring Webflow部分验证不起作用

史蒂夫

我正在尝试在Spring Webflow 2.4中实现部分验证。

我看过Spring Webflow部分验证和那里提供的解决方案-但这对我不起作用。添加组时,验证根本不会生效(但没有组就可以正常工作)。

我有如下代码和配置:

流配置

<view-state id="page1" view="apply/page1" model="myModel">
    <binder>
        <binding property="foo" />
    </binder>
    <transition on="next" to="page2" validate="true" validation-hints="'group1'"></transition>
</view-state>

<view-state id="page2" view="apply/page2" model="myModel">
    <binder>
        <binding property="baz" />
    </binder>
    <transition on="next" to="page3" validate="true" validation-hints="'group2'"></transition>
</view-state>

<view-state id="page3" view="apply/page3" model="myModel">
    <on-render>
        <set name="requestScope.foo" value="myModel.foo" />
        <set name="requestScope.baz" value="myModel.baz" />
    </on-render>
</view-state>

模型类

public class MyModel implements Serializable {

    @NotBlank(message = "Bad foo!", groups = {Group1.class})
    private String foo;

    @NotBlank(message = "Bad baz!", groups = {Group2.class})
    private String baz;

    ...//getters and setters

    public interface Group1 {
    }

    public interface Group2 {
    }
}

像这样的html视图:

<form th:object="${myModel}" th:action="${flowExecutionUrl}" method="post">

    <div class="error" th:if="${#fields.hasErrors('*')}">
    <span th:each="err : ${#fields.errors('*')}">
        <span th:text="${err}">Input is incorrect</span>
    </span>
    </div>

    <input type="text" th:field="*{foo}"/>

    <button type="submit" name="_eventId_next">Next</button>

</form>

<form th:object="${myModel}" th:action="${flowExecutionUrl}" method="post">

    <div class="error" th:if="${#fields.hasErrors('*')}">
    <span th:each="err : ${#fields.errors('*')}">
        <span th:text="${err}">Input is incorrect</span>
    </span>
    </div>

    <input type="text" th:field="*{baz}"/>

    <button type="submit" name="_eventId_next">Next</button>

</form>

我的意图是在构成向导流的多个视图中使用单个模型作为支持表单-在我进行过程中进行部分验证。但是,当我介绍实现此目标所需的组时,验证不会在流程中的任何时候触发。

如果我在验证提示名称中输入错别字,则会出现错误-例如:

<transition on="next" to="page2" 
    validate="true" validation-hints="'group1zzzz'"></transition>

结果是

Failed to resolve validation hint [group1zzzz]

所以这些团体被接走了。但是验证并没有发生,我可以遍历数据输入而不会触发错误。

我正在使用spring-webflow 2.4.2.RELEASE

有什么想法,如果我错过了什么,或者这只是行不通?

更新

我已经做了一些深入研究org.hibernate.validator.internal.engine.ValidatorImpl的代码-它决定是否需要验证:

private boolean isValidationRequired(ValidationContext<?> validationContext,
        ValueContext<?, ?> valueContext,
        MetaConstraint<?> metaConstraint) {
    if ( validationContext.hasMetaConstraintBeenProcessed(
            valueContext.getCurrentBean(),
            valueContext.getPropertyPath(),
            metaConstraint
    ) ) {
        return false;
    }

    if ( !metaConstraint.getGroupList().contains( valueContext.getCurrentGroup() ) ) {
        return false;
    }
    return isReachable(
            validationContext,
            valueContext.getCurrentBean(),
            valueContext.getPropertyPath(),
            metaConstraint.getElementType()
    );
}

在:

    if ( !metaConstraint.getGroupList().contains( valueContext.getCurrentGroup() ) ) {
        return false;
    }

我看到了我的组的实例-例如,两个集合中的“ MyModel $ Group2”,但它们似乎不是同一对象实例,因此,“ Collection.contains()”失败,并且isValidationRequired()始终返回false。查看屏幕截图。

在此处输入图片说明

[![在此处输入图片描述] [2]] [2]

Agggggggggghhhhhhh-发现问题的根源

结合使用spring-boot-devtools,似乎还有另一个类加载器正在参与加载组标记器接口的其他实例。这使得上面的“ Collection.contains()”每次都失败。

如果我从构建堆栈中取出spring-boot-devtools,它就可以工作。现在的问题变成:

“如何让验证组与spring-boot-devtools一起使用?”

在此处输入图片说明

史蒂夫

好吧,毕竟,如果您混合使用了spring-boot-devtools,那么该webflow会导致类加载器出现一些问题。修复似乎是要创建文件:

/WEB-INF/spring-devtools.properties

并将webflow jar添加到包含列表:

restart.include.webflow=/spring-webflow-2.4.2.RELEASE.jar

这似乎使webflow使用相同的devetools类加载器,并且在查找验证提示接口时,验证现在可以正常进行了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章