Spring Boot - MVC 1

 

MVC : Model View Controller이라는 하나의 디자인 패턴

Spring MVC : 스프링이 제공하는 웹 전용 MVC Framework

 

Model : 비즈니스 로직 처리

View : 사용자가 보는 UI

Controller : Model과 View 사이에서 데이터를 처리


 

index

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>
<body>
<p>Hello Spring</p>
<a href="/hello">Hello Page</a>
</body>
</html>

 

Controller

package hello.hellospring.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;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {

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

 

View

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

 

 

Controller : 컨트롤러의 역할을 할 것이라고 명시하는 어노테이션

 

http://localhost:8080 >

 

 

index 페이지에서 Hello Page를 클릭하면

GetMapping을 통해 비즈니스 로직을 호출하고 json 형식으로 data를 반환합니다.

 

http://localhost:8080/hello >

 

타임리프를 통해 전달받은 data를 출력해 줍니다.

'Java > Springboot' 카테고리의 다른 글

Spring Boot - jsp 사용하기  (0) 2025.02.25
Spring Boot - 빌드하고 실행하기  (1) 2025.02.21
Spring Boot - API  (0) 2025.02.21
Spring Boot - MVC 2  (0) 2025.02.21
Spring Boot - 프로젝트 생성  (1) 2025.02.20