[Error] 해결 org.springframework.validation.BindException: org.springframework.val

2022. 8. 18. 17:09·🍀 Spring Boot

Validation을 이용하여 값이 입력되었는지 등등을 설정해주었다.

@AllArgsConstructor
@Getter
public class QuestionForm{

  @NotEmpty(message = "제목은 필수 항목입니다.")
  @Size(max = 200, message = "제목을 200자 이하로 입력해주세요.")
  private String subject;

  @NotEmpty(message = "내용은 필수 항목입니다.")
  private String content;
}

 
org.springframework.validation.BeanPropertyBindingResult
프로젝트를 실행시켜 값이 제대로 입력되지 않았을 경우, 에러 문구가 잘 뜨는지 확인하고자 했는데, 이러한 오류가 발생했다!
 
이유는, Controller에서 찾을 수 있었다!!

@PostMapping("/create")
public String questionCreate(@Valid QuestionForm questionForm, Model model, BindingResult bindingResult) {
    if (bindingResult.hasErrors()) {
        return "question_form";
    }

    questionService.create(questionForm.getSubject(), questionForm.getContent());
    return "redirect:/question/list";
}

@Valid 어노테이션이 붙은 questionform 뒤에 바로 BindingResult가 위치해야 오류가 발생하지 않는다.
 

@PostMapping("/create")
public String questionCreate(@Valid QuestionForm questionForm, BindingResult bindingResult, Model model) {
    if (bindingResult.hasErrors()) {
        return "question_form";
    }

    questionService.create(questionForm.getSubject(), questionForm.getContent());
    return "redirect:/question/list";
}

순서를 바꿔주어서 해결
 
https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-ann-arguments

Web on Servlet Stack

Spring Web MVC is the original web framework built on the Servlet API and has been included in the Spring Framework from the very beginning. The formal name, “Spring Web MVC,” comes from the name of its source module (spring-webmvc), but it is more com

docs.spring.io

반응형
저작자표시 (새창열림)
'🍀 Spring Boot' 카테고리의 다른 글
  • [Spring] 의존성 주입(DI, Dependency Injection) (생성자 주입을 사용해야 하는 이유)
  • [Spring] JPA N + 1 문제 발생 원인 및 해결 방안
  • [Spring] 스프링 입문 - 7. AOP
  • [Spring] 스프링 입문 - 6. 스프링 DB 접근 기술
dmaolon
dmaolon
프로그래밍을 공부한 내용을 기록하는 공간입니다.
  • dmaolon
    기록 남기기
    dmaolon
  • 전체
    오늘
    어제
    • ALL (260)
      • ➰ Series (5)
      • 🎯PS (168)
        • Algorithm (15)
      • ☕ Java (11)
      • 🍀 Spring Boot (29)
      • 💬 Database (9)
      • 🐣 Computer Science (14)
      • 👍 Daily (4)
      • 🎁ReactJS (4)
  • 인기 글

  • 최근 댓글

  • 최근 글

  • 태그

    프로그래머스
    알고리즘
    dfs
    자바
    프로그래밍
    BFS
    백준
    코딩
    파이썬
    Spring
  • hELLO· Designed By정상우.v4.10.1
dmaolon
[Error] 해결 org.springframework.validation.BindException: org.springframework.val
상단으로

티스토리툴바