Back-end/Spring

Spring 웹 개발 방식 3가지

calvin9150 2021. 3. 13. 05:27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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""hello!!");
        return "hello";
    }
 
    @GetMapping("hello-mvc")
    public String helloMvc(@RequestParam(value = "name"String name, Model model){
        model.addAttribute("name", name);
        return "hello-template";
    }
 
    @GetMapping("hello-string")
    @ResponseBody // HTTP에서의 body부에 밑 데이터를 직접 넣어주겠다는 뜻
    public String helloString (@RequestParam("name")String name){
        return "hello" + name;
    }
 
    @GetMapping("hello-api")
    @ResponseBody
    public Hello helloApi(@RequestParam("name"String name){
        Hello hello = new Hello(); // ctrl+shift+enter : 세미콜론 생성
        hello.setName(name);
        return hello;
    }
    //alt+insert : generate (getter/setter 생성가능)
    public class Hello{
        private String name;
 
        public String getName() {
            return name;
        }
 
        public void setName(String name) {
            this.name = name;
        }
    }
 
}
cs

 

 

정적 컨텐츠 : 서버에서 파일을 그대로 내려준다..

MVC와 템플릿엔진 : model, view, controller로 나눠서 렌더링 된 HTML을 클라이언트에게 준다.

API : 객체 반환!

 

jason : key-value 형식이용

 

alt+insert : generate (getter/setter 생성가능)

 

@ResponseBody 가 붙어있다!! -> 스프링컨테이너에서 HttpMessageConverter 가 처리해준다..

여기서 단순문자면 StringConverter, 객체면 JsonConverter 가 동작..