Spring 웹 개발 기초

지꾸이·2025년 2월 23일

Spring

목록 보기
1/6
  1. 정적 컨텐츠
  2. MVC와 템플릿 엔진
  3. API

정적 컨텐츠

원리
1.관련 컨트롤러를 찾아본다.
2. 없으면 resources:static.html이 있는지확인
3. 반환~

MVC와 템플릿 엔진

MVC : Mode, View, Controller

@GetMapping("hello-mvc")
    public String helloMvc(@RequestParam("name") String name, Model model) {
        model.addAttribute("name", name);
        return "hello-template";
    }
<p th:text="'hello ' + ${name}">hello! empty</p>

외부에서 파라미터를 받겠다. @Request Param
model(name:value --> ${name}
hello!empty는 default값이지만 th:text속성땜에 의미없음(th덮어쓰기)

API

  • JSON
  • 서버끼리 통신
@GetMapping("hello-string")
    @ResponseBody
    public String helloString(@RequestParam("name") String name) {
        return "hello" + name;
    }

@ResponseBody

http body부분에 데이터를 직접 추가해주겠다!는 의미
이전에는 템플릿을 통해 view로 출력해주는 형식이라면
이건 입력한 그대로 보여준다..
http://localhost:8080/hello-string?name=spring!! <이런 형태


이 어노테이션이 적혀있는데 어라..
//public Hello helloApi(@RequestParam("name") String name)
객체로 반환된다면??

  • 기존에는 viewResolver가 반응했지만 이건 HttpMessageConverter가 반응한다 //default값은 json방식이 된다(기본정책)
profile
백엔드

0개의 댓글