티스토리 뷰
Validator
ContentDto.java : Lombok 이용
createPage.jsp : Form 입력받는 페이지
createDonePage.jsp : 입력받은 값을 출력하는 페이지
@RequestMapping("/") : Validator (1) 출력
@RequestMapping("/insertForm") : createPage.jsp로 이동
@RequestMapping("/") : contentDto를 출력하고, validator에 에러가 있으면 createPage.jsp로, 에러가 없으면 createDonePage.jsp로 이동
ContentValidator.java
package com.study.springboot;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
public class ContentValidator implements Validator {
@Override
public boolean supports(Class<?> arg0) {
return ContentDto.class.isAssignableFrom(arg0); // 검증할 객체의 클래스 타입 정보
}
@Override
public void validate(Object obj, Errors errors) {
ContentDto dto = (ContentDto) obj;
String sWriter = dto.getWriter();
if (sWriter == null || sWriter.trim().isEmpty()) {
System.out.println("Writer is null or empty");
errors.rejectValue("writer", "trouble");
}
String sContent = dto.getContent();
if (sContent == null || sContent.trim().isEmpty()) {
System.out.println("Content is null or empty");
errors.rejectValue("content", "trouble");
}
}
}
ValidationUtils
이전 예제와 동일한 설정
rejectIfEmptyOrWhitespace() : null이거나 isEmpty면 errors에 key값으로 value를 넣는 메소드
initBinder
이전 예제와 동일한 설정
강한 결합을 이용하지 않고, @InitBinder와 @Valid를 이용한 의존성 주입
error를 상세하게 출력함
Valid Annotation
ContentValidator.java 자바 파일을 이용하지 않고 ContentDto.java에서 annotation을 이용하여 validation
@NotNull(message="writer is null.")
@NotEmpty(message="writer is empty.")
@Size(min=3, max=10, message="writer min 3, max 10.")
인프런 - 예제로 배우는 스프링부트 입문 강의를 듣고 정리한 내용
'spring boot' 카테고리의 다른 글
| JdbcTemplate (0) | 2021.05.12 |
|---|---|
| web 기초 (0) | 2021.05.12 |
| DI(Dependency Injection) (0) | 2021.05.12 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- sequence type
- springboot
- python flag
- 자료구조
- 선형 탐색
- postfix notation
- Django
- for-else
- djnago
- 양방향 연결 리스트
- tailwind
- python
- Stack
- 직접 주입
- divmod
- 스프링부트
- 스택
- initBinder
- 파이썬
- ValidataionUtils
- string module
- rjust
- 선형 배열
- 이진 탐색
- valid annotation
- ljust
- 의존 주입
- most_common
- airbnb clone
- 출력 형식 지정
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 |
글 보관함
