danDevlog

form태그 value="" 오류 (@ModelAttribute) BeanPropertyBindingResult 본문

오류 및 편의성 모음

form태그 value="" 오류 (@ModelAttribute) BeanPropertyBindingResult

단데기이 2022. 5. 26. 09:57
728x90
WARN : org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'criteria' on field 'amount': rejected value []; codes [typeMismatch.criteria.amount,typeMismatch.amount,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [criteria.amount,amount]; arguments []; default message [amount]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'amount'; nested exception is java.lang.NumberFormatException: For input string: ""]
Field error in object 'criteria' on field 'pageNum': rejected value []; codes [typeMismatch.criteria.pageNum,typeMismatch.pageNum,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [criteria.pageNum,pageNum]; arguments []; default message [pageNum]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'pageNum'; nested exception is java.lang.NumberFormatException: For input string: ""]]

해당오류는 get방식의 form태그에서 값을 가져오는 과정에서 value의 값이 "" 로 들어가서 생긴 오류이다.

 

오류 트레이스를 읽어보면 int타입값이 와야하는데 String 값이 들어왔다해서 해당 VO로 가서 다시보고, 

Mapper도 다시 보고 하면서 2시간정도 낭비했던것같다. 하지만 맨 마지막 부분에 For input string : "" 라는것을 보고

브라우저 페이지의 개발자 모드에 들어가서 form 태그 부분의 value값을 보니 다른건 값이 들어와있지만 criteria객체의 변수들만 없다는것을 알 수 있었다.

https://animal-park.tistory.com/86 이분의 글을 참고하였다.

 

// 변경전 (오류코드)
// 작가 상세 페이지
	@GetMapping({"/authorDetail", "/authorModify"})
	public void authorGetInfoGET(int authorId, Criteria cri, Model model) throws Exception{
		log.info("authorDetail..." + authorId);
		// 페이지 정보
		model.addAttribute("cri" + cri);
		
		
		// 선택 작가 정보
		model.addAttribute("authorInfo", authorservice.authorGetDetail(authorId));
		
	}

// 변경후
// 작가 상세 페이지
	@GetMapping({"/authorDetail", "/authorModify"})
	public void authorGetInfoGET(int authorId,  @ModelAttribute("cri") Criteria cri, Model model) throws Exception{
		log.info("authorDetail..." + authorId);
		// 페이지 정보
		// model.addAttribute("cri" + cri);
		
		
		// 선택 작가 정보
		model.addAttribute("authorInfo", authorservice.authorGetDetail(authorId));
		
	}

 

@ModelAddAttribute 와 model.addattribute의 차이점

https://dev-coco.tistory.com/100

Comments