Updated:

정적 컨텐츠

 스프링 부트는 src/main/resources/staric에서 정적 컨텐츠를 찾아 표현한다. 이 패키지에 hello-static.html을 생성한다.

<!DOCTYPE HTML>
<html>
<head>
    <title>static content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
정적 컨텐츠 입니다.
</body>
</html>

 이를 실행하면 localhost8080/hello-static.html에 위 html파일이 실행된다.

MVC와 템플릿 엔진

MVC는 Model, View, Controller을 의미한다. Model과 Controller는 비지니스 로직이나 내부적(서버)으로 처리하고 View는 화면을 그린다. HelloController.java를 다음과 같이 수정한다.

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;

@Controller
public class HelloController {

    @GetMapping("hello")
    public String hello(Model model){
        model.addAttribute("data", "spring3!!");
        return "hello";
    }
    
    @GetMapping("hello-mvc")
    public String helloMvc(@RequestParam("name") String name, Model model) { // RequestParam은 파라미터를 외부에서 받는 것이다.
        model.addAttribute("name", name);
        return "hello-template";
    }
}

 그리고 hello-template.html을 생성한다.

<html xmlns:th="http://www.thymeleaf.org"> <!-- thymeleaf 템플릿 엔진 사용 -->
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>

 localhost8080/hello-mvc를 들어가면 에러가 발생한다. 이는 name의 파라미터의 값을 받지 못했기 때문이다. 그래서 localhost8080/hello-mvc?name=spring처럼 Get방식으로 실행한다.

API

 HelloController.java를 다음과 같이 수정한다.

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", "spring3!!");
        return "hello";
    }

    @GetMapping("hello-mvc")
    public String helloMvc(@RequestParam("name") String name, Model model) { // RequestParam은 파라미터를 외부에서 받는 것이다.
        model.addAttribute("name", name);
        return "hello-template";
    }

    @GetMapping("hello-string")
    @ResponseBody //http 통신 프로토콜에서 body부를 직접 넣어준다.
    public String helloString(@RequestParam("name") String name) {
        return "hello " + name; // neme을 sping으로 넣어주면 "hello spring"이 된다.
    }
}

 위를 수정한 뒤 실행해보면 localhost8080/hello-static?name=spring으로 가면 html이 아닌 hello spring만 담긴 화면을 볼 수 있다. HelloController을 다시 다음과 같이 수정한다.

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", "spring3!!");
        return "hello";
    }

    @GetMapping("hello-mvc")
    public String helloMvc(@RequestParam("name") String name, Model model) { // RequestParam은 파라미터를 외부에서 받는 것이다.
        model.addAttribute("name", name);
        return "hello-template";
    }

    @GetMapping("hello-string")
    @ResponseBody //http 통신 프로토콜에서 body부를 직접 넣어준다.
    public String helloString(@RequestParam("name") String name) {
        return "hello " + name; // neme을 sping으로 넣어주면 "hello spring"이 된다.
    }

    @GetMapping("hello-api")
    @ResponseBody // 객체가 오면 JSON으로 반환
    public Hello helloApi(@RequestParam("name") String name) {
        Hello hello = new Hello();
        hello.setName(name);
        return hello;
    }

    static class Hello {
        private  String name;

        public String getName() { // Alt + Insert를 통해 Getters and Setters, 이를 java bin 규약이라 한다.
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}

 localhost8080/hello-api?name=spring으로 접속을 하면 {“name”:”spring”}이 출력된다. 이를 JSON이라고 한다(XML).

댓글남기기