我的主要课程是
public class UserAddressesForm {
@NotEmpty
private String firstName;
@NotEmpty
private String lastName;
private List<AddressForm> addresses;
...
setters and getters
public class AddressForm {
@NotEmpty
private String customName;
@NotEmpty
private String city;
@NotEmpty
private String streetAn;
@NotEmpty
private String streetHn;
@NotEmpty
private String addressCountry;
@NotEmpty
private String postCode;
...
setters and getters
控制器
@RequestMapping(value = "/up", method = RequestMethod.POST)
public String completeForm(@Valid @ModelAttribute("userAddressesForm") UserAddressesForm userAddressesForm,
BindingResult result, HttpServletRequest req)
...
JSP页面
<form:form commandName="userAddressesForm" action="registered">
<table>
<tr>
<td class="formLabels"><form:label path="firstName">
<spring:message code="label.name" />
</form:label></td>
<td><form:input path="firstName" /></td>
<td><form:errors path="firstName" cssClass="error" /></td>
</tr>
<tr>
<td class="formLabels"><form:label path="lastName">
<spring:message code="label.surname" />
</form:label></td>
<td><form:input path="lastName" /></td>
<td><form:errors path="lastName" cssClass="error" /></td>
</tr>
</table>
<c:forEach items="${userAddressesForm.addresses}" varStatus="gridRow">
<div id="main_address" class="address_data_form">
<fieldset>
<legend><spring:message code="label.stepThreeMainAddressInfo" /></legend>
<a href="#" class="deleteItem"></a>
<table>
<tr>
<td class="formLabels">
<spring:message code="label.address.custom.name" />
</td>
<td>
<spring:bind path="addresses[${gridRow.index}].customName">
<input type="input" name="<c:out value="${status.expression}"/>"
id="<c:out value="${status.expression}"/>"
value="<c:out value="${status.value}"/>" />
<form:errors path="${status.expression}"/>
</spring:bind>
</td>
</tr>
<tr>
<td class="formLabels">
<spring:message code="label.streetAnStreetHn" />
</td>
<td>
<spring:bind path="addresses[${gridRow.index}].streetAn">
<input type="input" name="<c:out value="${status.expression}"/>"
id="<c:out value="${status.expression}"/>"
value="<c:out value="${status.value}"/>" />
</spring:bind>
<spring:bind path="addresses[${gridRow.index}].streetHn">
<input type="input" name="<c:out value="${status.expression}"/>"
id="<c:out value="${status.expression}"/>"
value="<c:out value="${status.value}"/>" >
<form:errors path="addresses[${gridRow.index}].streetHn"/>
</spring:bind>
</td>
</tr>
<tr>
<td class="formLabels">
<spring:message code="label.postCode" />
</td>
<td>
<spring:bind path="addresses[${gridRow.index}].postCode">
<input type="input" name="<c:out value="${status.expression}"/>"
id="<c:out value="${status.expression}"/>"
value="<c:out value="${status.value}"/>" />
</spring:bind>
</td>
</tr>
<tr>
<td class="formLabels">
<spring:message code="label.city" />
</td>
<td>
<spring:bind path="addresses[${gridRow.index}].city">
<input type="input" name="<c:out value="${status.expression}"/>"
id="<c:out value="${status.expression}"/>"
value="<c:out value="${status.value}"/>" />
<form:errors path="addresses[${gridRow.index}].city" cssClass="error" />
</spring:bind>
</td>
</tr>
</table>
</fieldset>
</div>
</c:forEach>
为什么对象字段未经过验证的AddressForm?
请帮忙。
答案 0 :(得分:135)
您需要使用addresses
注释装饰UserAddressesForm
@Valid
成员。见JSR 303: Bean Validation的3.1.3和3.5.1节。正如我在回答问题Is there a standard way to enable JSR 303 Bean Validation using annotated method时所解释的那样,这是根据JSR 303真正使用@Valid
注释。
修改强> 示例代码:Hibernate Validator- Object Graph。 (车内乘客名单)
答案 1 :(得分:23)
添加到@Ritesh回答,@Valid
约束将指示Bean Validator深入研究其应用属性的类型并验证在那里找到的所有约束。回答问题的代码,验证者在@Valid
属性上看到addresses
约束时,将探索AddressForm
类并验证内部发现的所有JSR 303
约束,如下所示:
public class UserAddressesForm {
@NotEmpty
private String firstName;
@NotEmpty
private String lastName;
@Valid
private List<AddressForm> addresses;
...
setters and getters
public class AddressForm {
@NotEmpty
private String customName;
@NotEmpty
private String city;
@NotEmpty
private String streetAn;
@NotEmpty
private String streetHn;
@NotEmpty
private String addressCountry;
@NotEmpty
private String postCode;
...
setters and getters
答案 2 :(得分:3)
在UserAddressesForm类中,添加以下行
@Valid
private List<AddressForm> addresses;
答案 3 :(得分:0)
有效的解决方案。
public class UserAddressesForm {
@NotEmpty(message="firstName is required")
private String firstName;
@NotEmpty(message="lastNameis required")
private String lastName;
@NotNull(message="addresses attributes are required")
@Valid
private List<AddressForm> addresses;
...
setters and getters
public class AddressForm {
@NotEmpty(message="customNameis required")
private String customName;
@NotEmpty
private String city;
@NotEmpty
private String streetAn;
@NotEmpty
private String streetHn;
@NotEmpty
private String addressCountry;
@NotEmpty
private String postCode;
...
setters and getters