form에 다음과 같이 input 태그를 date타입으로 설정하고 제출하면 Date 타입으로 자동으로 변환되지 않는다.
<input type="date" name="date"/>
400 bad request 에러를 얻게 된다.
This application has no explicit mapping for /error, so you are seeing this as a fallback.
There was an unexpected error (type=Bad Request, status=400).
Validation failed for object='formDto'. Error count: 1
org.springframework.web.method.annotation.ModelAttributeMethodProcessor$1: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'formDto' on field 'date': rejected value [2022-06-07]; codes
date 입력 폼을 받는 Controller에 다음과 같은 코드를 넣으면 된다.
@InitBinder
private void dateBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new
SimpleDateFormat("yyyy-MM-dd"); //날짜 형식은 알아서 정하기
CustomDateEditor editor = new CustomDateEditor(dateFormat, true);
binder.registerCustomEditor(Date.class, editor);
}
How to read input type="date" to java object Date?
I have so input <input type="date" name="date"> How to read from this input to java object of class java.util.Date? P.S. Date date is a field of my Bean, which I read so: @RequestMappin...
stackoverflow.com
나는 DateUtil 클레스를 만들어서 필요한 컨트롤러에 extends하여 사용하고 있다.
public class CheckReqController extends DateUtil
'Java > Spring Boot' 카테고리의 다른 글
[SpringBoot] 서버 시작 시 에러 (1) | 2024.05.11 |
---|---|
[SpringBoot] Spring Seurity 정리 (0) | 2024.05.11 |
[Spring Boot] gmail 전송하기 (0) | 2024.05.11 |
[SpringBoot] CORS 대응 (0) | 2022.12.07 |
[SpringBoot] java.lang.NoClassDefFoundError: javax/xml/bind/DatatypeConverter (1) | 2022.11.17 |