gpt4 book ai didi

java - @Valid 注解不验证子对象列表

转载 作者:IT老高 更新时间:2023-10-28 11:44:30 25 4
gpt4 key购买 nike

主要模型类如下:

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
}

我的一个 Controller 中的一个端点:

@RequestMapping(value = "/up", method = RequestMethod.POST)
public String completeForm(@Valid @ModelAttribute("userAddressesForm") UserAddressesForm userAddressesForm,
BindingResult result, HttpServletRequest req) {

// logic here

}

一个 .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>

为什么 @Valid未验证 List<AddressForm> addresses出现在 UserAddressesForm上课?

最佳答案

你需要装饰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 的回答中所解释的那样,这才是@Valid的真正用途根据 JSR 303 进行注释。

编辑示例代码:Hibernate Validator- Object Graph . (车内乘客名单)

编辑来自Hibernate Validator 6引用文档:

In versions prior to 6, Hibernate Validator supported cascaded validation for a subset of container elements and it was implemented at the container level (e.g. you would use @Valid private List<Person> to enable cascaded validation for Person).

This is still supported but is not recommended. Please use container element level @Valid annotations instead as it is more expressive.

例子:

public class Car {

private List<@NotNull @Valid Person> passengers = new ArrayList<Person>();

private Map<@Valid Part, List<@Valid Manufacturer>> partManufacturers = new HashMap<>();

//...
}

还可以查看 Bean Validation 2.0/Jakarta Bean Validation 中的新功能.

关于java - @Valid 注解不验证子对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5142065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com