spring-mvc - 如何对嵌套列表对象进行 Hibernate 验证?

标签 spring-mvc nested-attributes hibernate-validator

我需要验证存储在表单 bean 对象列表中的对象。

下面是我的表单 bean 对象。

public class Role implements java.io.Serializable {

    // Fields    
    private int roleId;

    @NotBlank
    private String roleName;

    private boolean active;

    @Valid
    private List<Module> modules;

    // getters anfd setters
}

下面是我的对象,它出现在我的主要表单 bean 对象列表中
public class Module implements Serializable {

    private int id;

    @NotBlank
    private String moduleName;

    // other properties and getters and setters
}

下面是我的属性文件
# -- Role form -- 
NotBlank.role.roleName=Role Name can not be blank.
NotBlank.module.moduleName=Module Name can not be blank.

下面是我的 JSP 页面,表单由角色名称和可以添加到角色的模块组成。
    <table border="0" class="section_tbl2">
    <tr>
        <td width="150px" valign="top">
            <spring:message code="dmx.role.form.label.name"/>
        </td>
        <td width="10px">:</td>
        <td>
            <form:input class="txtinput" id="roleName" path="roleName" maxlength="50"/>      <form:errors path="roleName" cssClass="error"/>

        </td>
    </tr>
    <tr><td colspan="3" height="8px"></td></tr>

    <tr>
        <td width="150px" vAlign="top">
            Modules
        </td>
        <td width="10px" vAlign="top">:</td>
        <td>

            <table>
                <tr>
                    <td>
                        <input type="button" value="<spring:message code="dmx.role.form.button.addModule.label"/>" onclick="return addModuleRow();"></input>
                    </td>
                </tr>
                <tr><td>&nbsp;</td></tr>
            </table>

            <table cellpadding="0" cellspacing="0" border="0" class="tblstyle1" id="moduleTable">
                <thead>
                    <tr>
                        <th class="fst" width="200px">
                            <spring:message code="dmx.role.form.label.moduleName"/>
                        </th>
                        <th width="50px"><spring:message code="dmx.role.form.label.create"/></th>
                        <th width="50px"><spring:message code="dmx.role.form.label.update"/></th>
                        <th width="50px"><spring:message code="dmx.role.form.label.delete"/></th>
                        <th width="30px"></th>
                    </tr>
                </thead>
                <tbody id="moduleTBody">
                    <c:forEach items="${role.modules}" var="module" varStatus="status" >
                        <c:set var="moduleCounter" value="${status.index}"/>
                        <tr id="moduleRowId_${moduleCounter}">
                            <td class="fst txt-center">
                                <form:select onchange="checkIfThisModuleAlreadySelected(this);" class="seloption" id="selectedModule_${moduleCounter}" path="modules[${moduleCounter}].id">
                                    <form:option value="" label="-- Select Module --"/>
                                    <form:options items="${moduleList}" itemLabel="moduleName" itemValue="id" />
                                </form:select>
                            </td>
                            <td class="txt-center">
                                <form:checkbox id="create_${moduleCounter}" path="modules[${moduleCounter}].create"/>
                            </td>
                            <td class="txt-center">
                                <form:checkbox id="update_${moduleCounter}" path="modules[${moduleCounter}].update"/>
                            </td>
                            <td class="txt-center">
                                <form:checkbox id="delete_${moduleCounter}" path="modules[${moduleCounter}].delete"/>
                            <td class="txt-center">
                                <input class="delbtn" id="moduleDelBtn_${moduleCounter}" name="moduleDelBtn[${moduleCounter}]" type="button" onclick="delModuleRow(${moduleCounter});">
                            </td>
                        </tr>
                    </c:forEach>
                </tbody>    
            </table>                
        </td>
    </tr>
    <tr><td colspan="3" height="3px"></td></tr>
</table>

我可以成功验证角色名称,即当角色名称为空白时,我收到一条错误消息,但是当未选择模块时,我没有收到任何错误消息。

请帮忙

最佳答案

添加 @NotNull@Size对您的模块列表的限制应该有助于:

@Valid
@NotNull
@Size(min = 1)
private List<Module> modules;
@Valid注释会导致被注释集合的元素被验证,但它不会验证该集合是否为空或包含任何元素。

关于spring-mvc - 如何对嵌套列表对象进行 Hibernate 验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7525593/

相关文章:

ruby - 无法将 Symbol 转换为 Integer + Rails 3.2 嵌套属性

使用@valid where/how 自定义错误消息进行 Spring 验证

spring-boot - 在weblogic服务器中部署spring boot应用程序时如何解决javax.validation的NoSuchMethodError?

java - Hibernate 验证 - 使用 @GroupSequence 对嵌套类进行分组

java - Spring 是从众多接口(interface)实现中单独挑选出一个接口(interface)实现吗?

java - 返回并转换 ResponseEntity<List<T>>

ruby-on-rails - 如何在嵌套属性中使用条件验证 validate_presence_of ?

java - 对每个请求进行用户身份验证 - Spring MVC

java - org.hibernate.id.IdentifierGenerationException : ids for this class must be manually assigned before calling save():

ruby-on-rails - 将嵌套属性与设计一起使用时的质量分配警告