2011-09-23 32 views
7

Tôi cần phải xác thực các đối tượng được lưu trữ trong danh sách trên đối tượng bean mẫu của tôi.Làm cách nào để xác thực Hibernate trên các đối tượng danh sách lồng nhau?

Dưới đây là đối tượng bean dạng của tôi.

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 
} 

và dưới đây là đối tượng của tôi mà có mặt trong danh sách các đối tượng đậu hình thức chính của tôi

public class Module implements Serializable { 

    private int id; 

    @NotBlank 
    private String moduleName; 

    // other properties and getters and setters 
} 

Dưới đây là tài sản của tôi nộp

# -- Role form -- 
NotBlank.role.roleName=Role Name can not be blank. 
NotBlank.module.moduleName=Module Name can not be blank. 

Dưới đây là trang JSP của tôi, biểu mẫu bao gồm tên vai trò và các mô-đun có thể được thêm vào vai trò.

<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> 

Tôi có thể xác thực thành công tên vai trò tức là khi tên vai trò trống, tôi nhận được thông báo lỗi nhưng khi không chọn mô-đun, tôi không nhận được thông báo lỗi.

Xin giúp

Trả lời

14

Thêm @NotNull@Size trở ngại đối với danh sách module bạn nên giúp:

@Valid 
@NotNull 
@Size(min = 1) 
private List<Module> modules; 

Các @Valid chú thích gây ra các yếu tố của bộ sưu tập có chú thích để được xác nhận nhưng nó không xác nhận thời tiết mà bộ sưu tập không phải là rỗng hoặc chứa bất kỳ phần tử nào.

Các vấn đề liên quan