如何在spring webflow中验证集合?

时间:2012-09-11 12:53:23

标签: java spring validation spring-webflow-2

我正在使用Spring WebFlow

我需要检查我是否至少有15个类型的练习集合,如果不是,我无法转到下一个流程。

我的注册流程:

<view-state id="practices" view="RegisterPractices" model="labs">
        <transition on="add" to="createNewPractice"></transition>
        <transition on="next" to="items" validate="true"></transition>
        <transition on="back" validate="false" to="owners"></transition>
</view-state>
<subflow-state id="createNewPractice" subflow="addPractice">
        <output name="practica" />      
        <transition on="practiceAdded" to="practices">
            <evaluate expression="labs.addPractice(currentEvent.attributes.practice)"></evaluate>
        </transition>       
        <transition on="practiceCancel" to="practices"></transition>
</subflow-state>

jsp实践:

<h2>Practices</h2>  
    <table class="standard-table" cellpadding="0" cellspacing="0">
        <thead>
            <tr>
                <th>Practice</th>
                <th>Operation</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <c:forEach items="${ labs.practices }" var="practice">
            <tr>
                <td>${ practice.practice}</td>
                <td><c:choose><c:when test="${ practice.realize == 1}">Realize</c:when><c:otherwise>Deriva</c:otherwise></c:choose></td>
            </tr>
            </c:forEach>
        </tbody>
    </table>
    <div>
        <a href="${flowExecutionUrl}&_eventId=add">Add a New Practice</a>
            <a href="${flowExecutionUrl}&_eventId=next">Back</a>
        <a href="${flowExecutionUrl}&_eventId=back">Next</a>
    </div>

View-State实践只是一个带有添加实践列表的jsp。

我尝试过使用customValidator,但我无法处理MessageBuilder.source(),因为我在该视图中没有任何对象。

我也尝试过决策状态,但是我无法显示“你必须选择至少15种做法才能继续”的消息

1 个答案:

答案 0 :(得分:0)

所以这个自定义验证器不起作用?

@Component
public class LabsValidator {

    public void validatePractices(final Labs labs,final ValidationContext context) {
        if(labs.getPractices().size() < 15) {
            context.getMessageContext().addMessage(new MessageBuilder().error().code("labs.practices.min15").build());
        }
    }

}