[오류해결]template might not exist or might not be accessible by any of the configured Template Resolvers

2022. 7. 7. 18:53·💻 개발/프레임워크
728x90

뷰에서 해당 게시글 삭제를 만들려고 하던 중 맞이한 오류이며 JPA queryDsl 작성중 맞이한 오류이다.

뷰 페이지에서 게시글 삭제 버튼 클릭 시 ajax를 통해 해당 게시글의 id를 보내주고, 

Controller에서 @Pathvariable로 id값을 받아서 DaoCustom, DaoCustomImpl로 가는 로직을 구상했다. 

 

그러던 중 아래와 같은 오류 메시지를 마주했다.

template might not exist or might not be accessible by any of the configured Template Resolvers

원인을 구글링해 본 결과 리턴해 줄 페이지가 없을 경우 경로를 찾지 못해 발생한 오류 라고 한다.

 

그러나 나는 @DeleteMapping을 사용하므로 리턴 페이지가 필요 없었고

페이지를 그릴 필요가 없는데 왜 오류가 나는 것이지??  라는 생각을 했다.

 

그러던 중 해결하게 되어 아래에 글을 작성하게 되었다.

 

1. 전체 로직을 살펴보자.


Q. 오류가 났을 때 내가 집중적으로 봤던 부분들이다. 이 말이 오류를 해결할 수 있는 힌트다.

write.html

//ajax 임시저장 삭제 로직 
function deleteDraftArticle(targetId) {
   if(!confirm("정말로 삭제하시겠습니까?")) {
      alert("취소되었습니다.");
   } else{$.ajax({
      url: "/v1/delete/" + targetId,
      method: "DELETE",
      success: function (result) {
         console.log("result", result);
         alert("삭제되었습니다.");
         reloadDraftArticles();
      },
      error: function(xhr, status, error) {
         var response = JSON.parse(xhr.responseText);
         if(response) {
            alert("요청 실패");
         }else {
            alert("API 호출 실패");
         }
      }
   });
   }
}

Controller

@DeleteMapping("/delete/{articleId}")
public void deleteDraftArticle(HttpServletRequest req, @PathVariable("articleId") Long articleId) {
   articleDao.deleteArticleById(articleId);
}

DaoCustom

void deleteArticleById(Long articleId);

DaoCustomImpl

@Override
@Transactional
public void deleteArticleById(Long articleId) {
   queryFactory.delete(bookClubArticle)
         .where(article.id.eq(articleId)
               .and(article.status.eq(ArticleStatus.WRITE)))
         .execute();
}

2. 문제해결


@Controller와 @RestController

Q. 오류가 났을 때 내가 집중적으로 봤던 부분들이다. 
위에 작성한 부분을 기억하는가? 

해답은 바로 @RestController가 아닌 @Controller로 선언된 Controller에서 작업했기 때문이다.


내가 이런 실수를 한 이유는

 

1. 나는 그동안 다뤘던 코드의 부분들은 다 @RestController가 선언된 파일들이였기 때문에 

당연히 이번에도 @RestController인 줄 알았다...

2. Controller를 만든 파일명이 article에 대해 선언된 Controller라 해당 파일에 만들면 될 줄 알았음 

 

즉 나의 경우는 @RestController에 선언된 곳에서 작업을 하지 않았기 때문에 발생한 오류 였다.

 

 

[@Controller가 아닌 @RestController의 차이]

@RestController는 이름에서도 알 수 있겠지만 

RestAPI를 만들 때 선언하는 어노테이션이다. 

나는 제이쿼리에서 지원하는 ajax 통신을 이용했기 때문에 @RestController를 선언한 곳에서 작업 했어야 했으나.

 

@Controller가 선언된 곳에서 만들었고 @Controller 였기 때문에

view가 있어야 한다고 오류가 나왔던 것이였다.

 

자세한 내용은 아래를 참고 하길 바란다.

2022.07.17 - 내가 헷갈려서 정리한 @RestController @Controller @Response @RequestParam @RequestBody @ModelAttribute정리

 

728x90
저작자표시 비영리 변경금지

'💻 개발 > 프레임워크' 카테고리의 다른 글

[Spring] application-oauth.yml를 .gitIgnore에 등록하지않고 보안을 유지하고, 깃허브 액션을 사용해서 빌드 방법  (0) 2023.01.05
[Spring] 내가 헷갈려서 정리한 @RestController @Controller @Response @RequestParam @RequestBody @ModelAttribute정리  (0) 2022.07.17
@Transactional에 대해 / @Transactional(readOnly = false) @Transactional(readOnly = true) 차이  (0) 2022.06.23
@PutMapping이용시 오류메시지 해결하기 / not-null property references a null or transient value  (0) 2022.05.27
[Spring] 스프링 DI 사용 시 생성자 주입을 사용하자!  (0) 2022.04.27
'💻 개발/프레임워크' 카테고리의 다른 글
  • [Spring] application-oauth.yml를 .gitIgnore에 등록하지않고 보안을 유지하고, 깃허브 액션을 사용해서 빌드 방법
  • [Spring] 내가 헷갈려서 정리한 @RestController @Controller @Response @RequestParam @RequestBody @ModelAttribute정리
  • @Transactional에 대해 / @Transactional(readOnly = false) @Transactional(readOnly = true) 차이
  • @PutMapping이용시 오류메시지 해결하기 / not-null property references a null or transient value
foodev
foodev
이것저것 개발과 이것저것 리뷰 합니다.
    250x250
  • foodev
    개발 개맛집
    foodev
  • 전체
    오늘
    어제
    • 분류 전체보기 (109)
      • 🌟🙇🏻‍♂️ 꼭 읽어봤으면 하는 글 (0)
      • 💻 개발 (76)
        • 설정 및 세팅 (4)
        • DB&서버&네트워크&암호 (12)
        • React (0)
        • JPA, Querydsl (14)
        • 알고리즘 (7)
        • 언어 (15)
        • 프레임워크 (14)
        • HTML, CSS (10)
      • ✍🏻 (33)
        • 회고록 (15)
        • 독서록 (7)
        • 일지록 (10)
        • 세미나 (1)
      • 💡 리뷰 (0)
        • 제품리뷰 (0)
  • 인기 글

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
foodev
[오류해결]template might not exist or might not be accessible by any of the configured Template Resolvers
상단으로

티스토리툴바