Validation(검증) 보충자료
BindingResult
주요 역할
1. 데이터 바인딩 결과 확인
2. 검증(Validation) 결과 처리
BindingResult 사용 방법
BindingResult 사용 방법예제: 폼 데이터 처리와 검증
public class User {
@NotEmpty(message = "이름은 필수입니다.")
private String name;
@Min(value = 18, message = "나이는 최소 18살 이상이어야 합니다.")
private int age;
// Getters and Setters
}주요 메서드
1. 에러 확인
2. 에러 정보 조회
3. 에러 메시지 접근
사용 시 주의사항
타임리프와 BindingResult를 활용한 검증 및 오류 처리
타임리프에서의 th:field
th:fielderrors.properties와 application.properties 설정
errors.properties와 application.properties 설정errors.properties8
application.properties
BindingResult와 Validator를 활용한 검증 (User 예제)
BindingResult에서 메시지 코드 처리
rejectValue와 reject
rejectValue와 reject메시지 코드 우선순위
예시 코드
검증 로직 분리
Validator 구현
컨트롤러에서 Validator 사용
@InitBinder 설정
@InitBinder 설정글로벌 Validator 설정
WebMvcConfigurer 사용
Thymeleaf와 함께 검증 오류 처리
Thymeleaf 템플릿
messages.properties
Last updated