Spring/스프링 입문

[김영한 스프링] 2. View 환경설정

개발게발 2023. 2. 1. 00:49

정적 페이지

Welcome Page 생성

<!DOCTYPE HTML>
<html>
<head>
    <title>Hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
Hello
<a href="/hello">hello321</a>
</body>
</html>

resources/static에 index.html 생성

 

 

동적 페이지

package hello.hellospring.controller;

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

@Controller
public class HelloController {
   
    @GetMapping("hello")
    public String hello(Model model){
        model.addAttribute("data", "hello!!");
        return "hello";
    }
}

@Controller

- controller임을 나타내는 역할

 

@GerMapping

- HTTP GET 요청이 왔을때 동작, URL에 매핑

 

Model

- Controller에서 생성된 데이터를 담아 View로 전달할때 사용

 

addAttribute

- key, value 형식

- Value를 Name 이름으로 View에 전달

 

return "hello"

- resources/templates/hello.html을 찾음

 

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'안녕하세요. ' + ${data}" >안녕하세요. 손님</p>
</body>
</html>

${data}

- controller에서 전달받은 Name을 ${}형태로 받음

 

 

동작원리

 

출처 : https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%EC%9E%85%EB%AC%B8-%EC%8A%A4%ED%94%84%EB%A7%81%EB%B6%80%ED%8A%B8

 

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

스프링 입문자가 예제를 만들어가면서 스프링 웹 애플리케이션 개발 전반을 빠르게 학습할 수 있습니다., - 강의 소개 | 인프런...

www.inflearn.com