서버/Spring boot

#Spring MVC

paran21 2022. 1. 26. 23:19

MVC?

  • MVC(Model - View - Controller) 디자인 패턴을 말한다.

 

Controller의 장점

  • HTTP request, response 처리를 위해 매번 작성해 줘야하는 중복코드들을 생략할 수 있다.
  • API마다 파일을 만들 필요가 없고, 유사한 성격의 API를 하나의 Controller로 관리할 수 있다.
  • Controller 내 메소드 명도 자유롭게 사용할 수 있다(Handler mapping에서 API - Controller 매칭)

 

서버에서 HTML을 내려주는 경우

1. 정적(static) 웹페이지

  • Controller는 요청을 Model로 받아서 처리하고, Client에게 View(정적 웹페이지, HTML)을 내려준다.

2. 동적(dynamic) 웹페이지

  • Controller는 요청을 Model로 받아서 처리하고, Template engine에게 View(동적 HTML 파일)와 Model(View에 적용할 정보들, 위의 Model과는 다름)을 전달한다.
  • 템플릿 엔진은 View에 Model을 적용하여 동적 웹페이지를 생성한다.
  • Client에게 View(동적 웹페이지, HTML)를 내려준다.

 

Controller와 HTTP Response 메시지

HTTP 참고자료 : https://developer.mozilla.org/ko/docs/Web/HTTP/Messages

Controller HTTP Response
@ResponseBody Return 타입 Return 값 Header Body
X Spring "{View name}" Content-Type:text/html View(HTML)의 text 내용
(타임리프) /templates/{View name}.html
"redirect:/{redirect URL}" Location: {Host URL}/{redirect URL} 없음
O String "{Text}" Content-Type:text/html "{Text}"
String  외 Java 객체 Content-Type:application/json JSON
Spring이 java 객체를 JSON으로 변화시켜줌

 

Controller와 HTTP Request 메시지

Controller HTTP Sample Request
Annotation 생략가능 Sample Code
@PathVariable x 파일 참조!!
@RequestParam o
o
@ModelAttribue o
@RequestBody x

 

'서버 > Spring boot' 카테고리의 다른 글

#Spring security  (0) 2022.01.28
#DI, IoC, Bean  (0) 2022.01.28
#H2연동하기  (0) 2022.01.26
#Model과 ModelAndView  (0) 2022.01.26
#자바에서 JSON형식 사용하기  (0) 2022.01.25