@RequestParam-템플릿 에러
2024. 8. 4. 18:51ㆍSPRING/접했던 오류
개발환경
jdk_21
Spring Framework
Thymeleaf
에러 내용
Error resolving template [request-param], template might not exist or might not be accessible by any of the configured Template Resolvers
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [request-param], template might not exist or might not be accessible by any of the configured Template Resolvers
- request-param 요청을 처리할 템플릿이 없거나, 접근할 수 없어 에러 발생.
원인
1. @Controller 사용
2. Controller 내 메서드에서 반환값 void 사용
3. 에러 내용과 같이 thymeleaf 에서 접근할 템플릿이 없음
@Controller
public class examController{
@GetMapping("request-param")
public void practiceRequestParamAnno(@RequestParam(required = false, value = "param") String param) {
System.out.println("param = " + param);
}
}
1. @Controller
@Controller 의 목적은 클라이언트의 요청을 받아, view 를 반환해주는 것이다.
2. void 사용
따라서, 2와 같이 void 를 사용하는 것은 적합하지 않다.
3. thymeleaf
하물며, 서버에서 요청에 대한 응답 내용을 view에 엮어주는 템플릿엔진 thyemleaf 를 사용하여 view 반환이 필수가 됨.
해결 방법
1. 템플릿 엔진이 정상작동 하도록 함.
thymeleaf 의 동작 원리에 맞게 String 값을 변환
2. 컨트롤러 클래스 레벨에서 @RestController 사용 혹은 메서드 레벨에서 @ResponseBody를 사용함.
@Controller의 목적은 view 를 반환하는 것.
view 를 제공할 필요 없이 요청 처리, 혹은 데이터만 전달받하고 싶다면 @RestController 를 사용함으로서 문제 해결 가능함.
@RestContorller 에는 @ResponseBody가 포함되는데 이를 통해 thymeleaf 의 간섭을 받지 않게 됨.
thymeleaf 목적 : view template 랜더링 및 전달
@responseBody 목적 : 데이터 전달
'SPRING > 접했던 오류' 카테고리의 다른 글
| H2 db - 예약어 (0) | 2024.08.07 |
|---|---|
| @RequestBody (0) | 2024.08.06 |
| @RequestParam - 파라미터 미기입 (0) | 2024.08.04 |
| 자바 버전 오류 (0) | 2024.08.03 |
| spring-boot-devtools 초기 설정 오류 (0) | 2024.08.03 |