[Spring 기초] MVC와 템플릿 엔진

Max·2023년 1월 4일
0

Spring

목록 보기
6/7


MVC - Model, View, Controller

Controller

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HelloController {
	
    @GetMapping("hello-mvc") {
    public String helloMvc(@RequestParam("name") String name, Model model) {
    	model.addAttribute("name", name);
        return "hello-template";
    }
}
  • @RquestParam()은 외부에서 데이터를 가져온다.
  • Model에 데이터를 담는다.
  • hello-temlpate으로 return

View

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>

실행

localhost:8080/hello-mvc --> whitelabel error page
※ 파라미터 name 값을 입력하지 않아서 발생하는 에러

localhost:8080/hello-mvc?name=spring
입력하면 정상적으로 spring이 출력이 된다.

profile
co_der

0개의 댓글

관련 채용 정보