Dev 개발 스터디 (동면 중)/Dev_log+Study 😶‍🌫️

[JAVA] addAttribute 에러 : 모델Model 객체 사용법

에이미쉬 2023. 2. 25. 15:40
728x90
반응형

[JAVA] addAttribute 에러

원본 소스 코드

  @RequestMapping(value = "/reviewList.do")
  public String reviewList(ReviewVO review) throws Exception {
    List<ReviewVO> reviewList = null;
    try {
      reviewList = reviewService.selectReviewList();
    } catch (Exception e) {
      e.printStackTrace();
    }
    review.addAttribute("reviewList", reviewList);

    return "contents/reviewList.tiles";
  } // end reviewList method

이슈 문구

 

The method addAttribute(String, List<ReviewVO>) is undefined for the type ReviewVO


이슈 원인

스프링 컨트롤러Controller, 모델 Model 객체 자리에 VO 파일명을 기재함

 

해결 방법

  1. 잘 못 기재한 객체명을 정정했다

 

변경 소스 코드

  @RequestMapping(value = "/reviewList.do")
  public String reviewList(ModelMap model) throws Exception {
    List<ReviewVO> reviewList = null;
    try {
      reviewList = reviewService.selectReviewList();
    } catch (Exception e) {
      e.printStackTrace();
    }
    model.addAttribute("reviewList", reviewList);

    return "contents/reviewList.tiles";
  } // end reviewList method

 

후기

또 다른 에러가 발생됐다. 나 울어

ModelMap cannot be resolved to a type

 

*출처 : https://rwd337.tistory.com/164

 

CRUD를 사용해 게시판 만들기(2) 리스트 만들기

게시판을 만들기전에 게시판 기능을 구현할수 있는부트스트랩 템플릿이나 디자인이들어간 간단한 홈페이지를 준비하자. 게시판을 만들기 위해 먼저 테이블을 만들어주자. 테이블의 컬럼은 게

rwd337.tistory.com

 


model 객체

  • Controller에서 생성한 데이터를 담아서 View로 전달할 때 사용하는 객체.
  • Servelt의 request.setAttribute()와 유사한 역할.
  • addAttribute("키", "값") 메소드를 사용하여 전달할 데이터 세팅.

@ModelAttribute

  • 강제로 전달받은 파라미터를 Model에 담아서 전달하도록 할 때 필요한 어노테이션
  • 스프링에서 Java beans 규칙(Getter, Setter, 생성자 포함)에 맞는 객체는 파라미터 전달이 자동으로 가능.
  • 하지만 일반 변수의 경우, 자동 전달 불가능. model 객체를 통해서 전달 필요

*출처 : https://iamdaeyun.tistory.com/entry/%EC%8A%A4%ED%94%84%EB%A7%81-%EC%BB%A8%ED%8A%B8%EB%A1%A4%EB%9F%AC#:~:text=Model%20addAttribute(Object%20value)&text=%2D%20value%EA%B0%80%20%EB%B0%B0%EC%97%B4%EC%9D%B4%EA%B1%B0%EB%82%98%20%EC%BB%AC%EB%A0%89%EC%85%98,%EC%9E%90%EB%8A%94%20%EC%86%8C%EB%AC%B8%EC%9E%90%EB%A1%9C%20%EC%B2%98%EB%A6%AC%ED%95%9C%EB%8B%A4. 

 

스프링 컨트롤러Controller, 모델Model 객체 사용법

스프링 컨트롤러Controller, 모델Model 객체 사용법 : 아래 코드 주석 참고. @RequestMapping(value="patternTrend.do") // url pattern mapping public String patternTrend(Model m) { // Model : 데이터를 담는 그릇 역할, map 구조로

iamdaeyun.tistory.com

 

728x90
반응형