JSR-303 @Valid 주석이 자식 개체 목록에 대해 작동하지 않습니다.
내 주요 수업은
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의 유효성을 검사하지 않는 이유는 무엇입니까?
도와주세요.
의 addresses
멤버 UserAddressesForm
를 @Valid
주석 으로 장식해야합니다 . JSR 303 : Bean Validation 의 섹션 3.1.3 및 3.5.1을 참조하십시오 . Annotated method를 사용하여 JSR 303 Bean Validation을 활성화하는 표준 방법이 있습니까? 라는 질문에 대한 답변에서 설명했듯이 JSR 303에 따른 @Valid
주석 의 실제 사용입니다 .
예제 코드 편집 : Hibernate Validator- Object Graph . (자동차 탑승객 목록)
Adding to @Ritesh answer, @Valid
constraint will instruct the Bean Validator to delve to the type of its applied property and validate all constraints found there. Answer with code to your question, the validator, when seeing a @Valid
constraint on addresses
property, will explore the AddressForm
class and validate all JSR 303
constraints found inside, as follows:
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
In the class UserAddressesForm add the following lines
@Valid
private List<AddressForm> addresses;
Working solution.
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
'Development Tip' 카테고리의 다른 글
Windows의 Ubuntu (WSL)에서 내 VS Code 터미널에 Bash를 사용하려면 어떻게해야합니까? (0) | 2020.10.04 |
---|---|
jQuery : 버튼을 제외한 첫 번째 보이는 입력 / 선택 / 텍스트 영역을 찾는 방법은 무엇입니까? (0) | 2020.10.04 |
뒤로 버튼 및 이전 활동 새로 고침 (0) | 2020.10.04 |
날짜 문자열을 다른 형식으로 변환하는 방법 (0) | 2020.10.04 |
응용 프로그램 구성 파일의 연결 문자열 'MyConnection'에 필수 providerName 특성이 없습니다. " (0) | 2020.10.04 |