본문 바로가기
스프링 Spring/스프링 입문

[스프링 웹 개발 기초] MVC와 템플릿 엔진

by 코딩개발 2022. 1. 9.
728x90
반응형

MVC와 템플릿 엔진

 

view : 화면을 그리는데 모든 역량을 집중

Controller : 비즈니스 로직, 내분 처리에 집중

 

 

resource > templates > hello-template.html 생성

 

<!-- hello-template.html -->
<html xmlns:th="http://www.thymeleaf.org">
<body>
<!-- 템플릿 엔진으로 치환되면 th:text 실행 / 그냥 실행시 hello! empty 실행 -->
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
// HelloController.java
@GetMapping("hello-mvc") // @RequestParam : 외부에서 파라미터를 받을거다
public String helloMvc(@RequestParam("name") String name, Model model) {
    model.addAttribute("name", name);
    return "hello-template";
}

 

결과

 

 

로그를 확인 하면 아래와 같은 WARN을 발견할 수 있다.

WARN 27040 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required request parameter 'name' for method parameter type String is not present]

 

에러 페이지가 나온 이유

  ---> HelloController@RequestParam("name") String name 부분이 있는 것처럼 name 파라미터를 넘겨받아야

        하는데 아무것도 넘겨받지 못해 에러 발생

 

 

해결 방법 : 아래와 같이 name 값을 넘겨주면 된다.

  ---> http://localhost:8080/hello-mvc?name=spring!!!

 

 

 

 


<출처>

인프런 강의 "스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술"을 정리한 것 입니다.

 

[무료] 스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술 - 인프런 | 강의

스프링 입문자가 예제를 만들어가면서 스프링 웹 애플리케이션 개발 전반을 빠르게 학습할 수 있습니다., 스프링 학습 첫 길잡이! 개발 공부의 길을 잃지 않도록 도와드립니다. 📣 확인해주세

www.inflearn.com

 
728x90
반응형

댓글