본문 바로가기
Spring/스프링_정리

[인프런-스프링입문] 스프링 웹 개발 기초

by 숭늉다섯 2023. 12. 13.

 

1. 정적컨텐츠

- 파일을 그대로 고객한테 웹브라우저를 전달

-> Spring.io 가면 정적컨테츠

 

 

2. MVC와 템플릿 엔진

- 서버에서 변형을 하여 HTML을 바꿔 주는 방식

 

 

3. API

- JSON과 같은 데이터 구조 포맷으로 클라이언트에게 데이터를 전달하는 방식

 

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-temlpete";
    }

    @GetMapping("hello-String")
    @ResponseBody
    public String helloString(@RequestParam(value = "name") String name) {
        return "Hello" + name;
    }

    @GetMapping("hello-api")
    @ResponseBody
    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() {
            return name;
        }

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

}